Differences between C# and Java cont'd -- Variables and operators
Variables and Operators Data Types
Brief Similarity on variables and operators
Both C# and Java are Type-Safe LanguagesJava and C# were designed to be type-safe. An illegal cast will be caught at compile time if it can be shown that the cast is illegal; or an exception will be thrown at runtime if the object cannot be cast to the new type. Type safety is therefore important because it not only forces a developer to write more correct code, but also helps a system become more secure from unscrupulous individuals.
Unified type system
Both languages are statically typed with class-based object orientation. In Java the primitive types are special in that they are not object-oriented and they could not have been defined using the language itself. They also do not share a common ancestor with reference types. The Java reference types all derive from a common root type. C# has a unified type system in which all types (besides unsafe pointers[12]) ultimately derive from a common root type. Consequently, all types implement the methods of this root type, and extension methods defined for the object type apply to all types, even primitive int literals and delegates. Note, that unlike Java, this allows C# to support objects with encapsulation that are not reference types.
In Java, compound types are synonymous with reference types; methods cannot be defined for a type unless it is also a class reference type. In C# the concepts of encapsulation and methods have been decoupled from the reference requirement so that a type can support methods and encapsulation without being a reference type. Only reference types support virtual methods and specialization, however, both languages support a number of built-in types that are copied and passed by value rather than by reference. Java calls these types primitive types, while they are called simple types in C#. The simple/primitive types typically have native support from the underlying processor architecture.
The C# primitive/simple types implement a number of interfaces and consequently offer a number of methods directly on instances of the types - even on the literals. The C# type names are also merely aliases for Common Language Runtime types. The C# System.Int64 type is exactly the same type as the long type; the only difference is that the former is the canonical .NET name while the latter is a C# alias for it.
Java does not offer methods directly on the primitive types. Instead methods that operate on the primitive values are offered through companion wrapper classes. A fixed set of such wrapper classes exist, each of which wraps one of the fixed set of primitive types. As an example, the Java Long type is a reference type that wraps the primitive long type. They are not the same type, however.
Advanced numeric types
Both languages offer library-defined arbitrary size integer types.
Only Java offers a data type for arbitrary precision decimal point calculations and only C# offers a type for working with complex numbers.
In both languages the number of operations that can be performed on the advanced numeric types are limited compared to the built-in IEEE 754 floating point types. For instance, none of the arbitrary size types support square root or logarithms.
C# allows library defined types to be integrated with existing types and operators by using custom implicit/explicit conversions and operator overloading.
Boxing and unboxing
Both languages allow automatic boxing and unboxing, i.e. they allow for implicit casting between any primitive types and the corresponding reference types.
In C#, the primitive types are subtypes of the Object type. In Java this is not true; any given primitive type and the corresponding wrapper type have no specific relationship with each other, except for auto boxing and unboxing.
Boxing
In situations where value types need to be treated as objects, the .NET and Java runtimes automatically converts value types to objects by wrapping them within a heap-allocated reference type in a process called boxing. The process of automatically convert an object to its corresponding value type such as converting an instance of java.lang.Integer to an int is known as unboxing. Below are examples of various situations where boxing occurs in both runtimes.
C# Code
using System;
using System.Collections;
//stack allocated structs also need to be boxed to be treated as objects
struct Point{
//member fields
private int x;
private int y;
public Point (int x, int y){
this.x = x;
this.y = y;
}
public override string ToString(){
return String.Format("({0}, {1})", x, y);
}
}
class ClashApp
{
public static void PrintString(object o)
{
Console.WriteLine(o);
}
public static void Main(string[] args)
{
Point p = new Point(10, 15);
ArrayList list = new ArrayList();
int z = 100;
PrintString(p);
PrintString(z);
list.Add(1);
list.Add(13.12);
list.Add(z);
for(int i =0; i < list.Count; i++)
PrintString(list[i]);
}
}
The code snippet above, illustrates the concept of boxing whereby the integer and float variable are boxed before they are stored in the list collection.
Java Code
import java.util.*;
class ClashApp
{
public static void PrintString(Object o){
System.out.println(o);
}
public static void PrintInt(int i){
System.out.println(i);
}
public static void main(String[] args){
Vector list = new Vector();
int z = 100;
Integer x = new Integer(300);
PrintString(z); //z boxed to object when passed to PrintString
PrintInt(x); //x unboxed to int when passed to PrintInt
// integers and float boxed when stored in collection
// therefore no need for Java wrapper classes
list.add(1);
list.add(13.12);
list.add(z);
for(int i =0; i < list.size(); i++)
PrintString(list.elementAt(i));
}
}
The code snippet above, also illustrates the concept of boxing whereby the integer and float variable are boxed before they are stored in the list collection.
COMMENTS
C#
Comments help in documenting the code of a program thus providing proper understanding of the functionality of the program.
C# supports three types of comments: single-line comments, multiline comments, and XML comments. The default C# XML documentation does not have analogs to Javadoc's @author, @version, or @deprecated tags although such metadata can be generated by reflecting on the assembly, as Microsoft's documentation build process does.
The primary benefit of an XML format is that the documentation specification can now be used in many different ways. XSLT stylesheets can then be used to convert the generated documentation to ASCII text, HTML, or Postscript files. Also of note is that the generated documentation can be fed to tools that use it for spec verification or other similar tasks. It should be noted that C# currently does not have a tool analogous to Javadoc for converting the XML documentation into HTML. Microsoft is in the process of developing such a tool which is currently codenamed SandCastle.
Xml comments{
/// <summary>
/// </summary>}
///<summary>Calculates the square of a number.</summary>
///<param name="num">The number to calculate.</param>
///<return>The square of the number. </return>
///<exception>NumberTooBigException - this occurs if the square of the number
///is too big to be stored in an int. </exception>
Java also supports three types of comments: single-line, double-line and Javadoc comments.
Javadoc is the tool used to extract API documentation from source code. Javadoc generates HTML documentation from the source code comment, below is a code snippet of Javadoc comments.
/** description tag
javadoc
/* block tag
/**
* Calculates the square of a number.
* @param num the number to calculate.
* @return the square of the number.
* @exception NumberTooBigException this occurs if the square of the number
* is too big to be stored in an int.
*/
Comments
Post a Comment