Contents - Cultural View

Contents - Cultural View Contents - Cultural View

culturalview.com
from culturalview.com More from this publisher
14.07.2013 Views

Comparison of Java and C Sharp 62 Bags/multisets Yes Yes Metadata Java C# Metadata annotations/attributes Interface based Class based Positional arguments No; unless a single argument Yes Named arguments Yes Yes Default values At definition Through initialization Nested types Yes Yes Specialization No Yes Conditional metadata No Yes Preprocessing, compilation and packaging Java C# Namespaces Packages Namespaces Packaging Package Assembly File contents Restricted Free Conditional compilation No Yes Custom errors/warnings No Yes Explicit regions No Yes Threading and synchronization Java C# Native interoperability Java C# External/native methods Yes Yes Marshalling External glue code required Yes; metadata controlled Pointers and arithmetics No Yes Native types No Yes Fixed size buffers No Yes Explicit stack allocation No Yes Address pinning (fixing) No Yes Address-of No Yes Object pinning (fix variable to address) No Yes Data types Unified type system 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. The Java reference types all derive from a common root type, however. C# has a unified type system in which all types 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, C# this way supports objects with encapsulation which are not reference types. In Java compound types are synonymous with reference types; you can not define methods 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.

Comparison of Java and C Sharp 63 The primitive types of Java are special and could not have been defined using the language itself. They also do not share a common ancestor with reference types. Simple/primitive types Both languages support a number built-in types which 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. C# has a few more primitive types than Java, due to the fact that it supports unsigned as well as signed integer types, and a decimal type for decimal floating-point calculations. Java lacks the unsigned types. In particular, Java lacks a primitive type for an unsigned byte. The Java byte type is signed, while the C# byte is unsigned and sbyte is signed. Both languages feature a native char (character) datatype as a simple type. Although the Java char type can used with bitwise operators, this is actually accomplished by promoting the char to an integer before the operation. C# has a type for high-precision (28 decimal digits) decimal arithmetic (based on the IEEE 754 Decimal128 format) for e.g. financial and monetary application. While Java lacks such a built-in type, the Java library does feature an arbitrary precision decimal type. This is not considered a language type and it does not support the usual arithmetic operators; rather it is a reference type which must be manipulated using the type methods. See more on arbitrary size/precision numbers below. 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# Long 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 in C# alias for it. Java does not offer methods directly on the primitive types. Instead methods which operate on the primitive values are offered through companion wrapper classes. A fixed set of such wrapper classes exist each of which wraps on of the fixed set of primitive types. As an example, the Java Long type is a reference type which wraps the primitive long type. They are not the same type, however. The following table summarizes the corresponding simple/primitive types of the languages: C# alias .NET CLR type Width (bits) Range (apx.) Java type Java wrapper sbyte SByte 8 -128 to 127 byte Byte byte Byte 8 0 to 255 Not available Not available short Int16 16 -32768 to 32767 short Short ushort UInt16 16 0 to 65535 Not available Not available int Int32 32 -2,147,483,648 to 2,147,483,647 int Integer uint UInt32 long Int64 ulong UInt64 32 0 to 4,294,967,295 Not 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 available 64 0 to 18,446,744,073,709,551,615 Not Not available long Long available Not available float Single 32 -3.402823e38 to 3.402823e38 float Float double Double 64 -1.79769313486232e308 to 1.79769313486232e308 double Double

Comparison of Java and C Sharp 63<br />

The primitive types of Java are special and could not have been defined using the language itself. They also do not<br />

share a common ancestor with reference types.<br />

Simple/primitive types<br />

Both languages support a number built-in types which are copied and passed by value rather than by reference. Java<br />

calls these types primitive types, while they are called simple types in C#. The simple/primitive types typically have<br />

native support from the underlying processor architecture.<br />

C# has a few more primitive types than Java, due to the fact that it supports unsigned as well as signed integer types,<br />

and a decimal type for decimal floating-point calculations.<br />

Java lacks the unsigned types. In particular, Java lacks a primitive type for an unsigned byte. The Java byte type is<br />

signed, while the C# byte is unsigned and sbyte is signed.<br />

Both languages feature a native char (character) datatype as a simple type. Although the Java char type can used<br />

with bitwise operators, this is actually accomplished by promoting the char to an integer before the operation.<br />

C# has a type for high-precision (28 decimal digits) decimal arithmetic (based on the IEEE 754 Decimal128 format)<br />

for e.g. financial and monetary application. While Java lacks such a built-in type, the Java library does feature an<br />

arbitrary precision decimal type. This is not considered a language type and it does not support the usual arithmetic<br />

operators; rather it is a reference type which must be manipulated using the type methods. See more on arbitrary<br />

size/precision numbers below.<br />

The C# primitive/simple types implement a number of interfaces and consequently offer a number of methods<br />

directly on instances of the types - even on the literals. The C# type names are also merely aliases for Common<br />

Language Runtime types. The C# Long type is exactly the same type as the long type; the only difference is that the<br />

former is the canonical .NET name while the latter is in C# alias for it.<br />

Java does not offer methods directly on the primitive types. Instead methods which operate on the primitive values<br />

are offered through companion wrapper classes. A fixed set of such wrapper classes exist each of which wraps on of<br />

the fixed set of primitive types. As an example, the Java Long type is a reference type which wraps the primitive<br />

long type. They are not the same type, however.<br />

The following table summarizes the corresponding simple/primitive types of the languages:<br />

C# alias .NET CLR type Width (bits) Range (apx.) Java type Java wrapper<br />

sbyte SByte 8 -128 to 127 byte Byte<br />

byte Byte<br />

8 0 to 255 Not<br />

available<br />

Not available<br />

short Int16 16 -32768 to 32767 short Short<br />

ushort UInt16<br />

16 0 to 65535 Not<br />

available<br />

Not available<br />

int Int32 32 -2,147,483,648 to 2,147,483,647 int Integer<br />

uint UInt32<br />

long Int64<br />

ulong UInt64<br />

32 0 to 4,294,967,295 Not<br />

64 -9,223,372,036,854,775,808 to<br />

9,223,372,036,854,775,807<br />

available<br />

64 0 to 18,446,744,073,709,551,615 Not<br />

Not available<br />

long Long<br />

available<br />

Not available<br />

float Single 32 -3.402823e38 to 3.402823e38 float Float<br />

double Double 64 -1.79769313486232e308 to 1.79769313486232e308 double Double

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!