Upload
virgil-snow
View
216
Download
1
Embed Size (px)
Citation preview
A Comparison of Java and C#
Peter JuszczykCS 492/493 - ISGS
First a test…
// Is this C# or Java?class TestApp { static void Main() { int counter = 0; counter++; }}
The answer is C# - In C# Main() always starts with an uppercase. In Java it is lowercase.
How is this relevant?
It should be "simple, object oriented, and familiar".
It should be "robust and secure". It should execute with "high
performance". It should be "architecture neutral
and portable".
Object Handling
Both C# and Java are designed from the ground up as VMT-based object oriented languages
Syntax similar to C++/C Both use garbage collection as a
means of reclaiming memory resources
Both include thread synchronization mechanisms as part of their language syntax
References
C# allows restricted use of pointers Code blocks or methods marked with the unsafe keyword
Compiler requires the /unsafe switch to allow compilation of a program that uses such code
Java does not allow pointers or pointer-arithmetic to be used The arguments to a method are passed
by value
Data Types
Both languages support the idea of primitive types Both treat Strings as immutable objects C# has more primitive types than Java -
unsigned as well as signed integer types supported
Both allow automatic boxing and unboxing to translate primitive data to and from their object form
Value Types
C# allows the programmer to create user-defined value types using the struct keyword Such value types are allocated on the
stack rather than on the heap Can be seen as lightweight classes Limitations: no inheritance
Java has no such corresponding concept
Enumerations
C# enums are derived from a primitive 8, 16, 32, or 64 bit integer type
Java enums are objects They are typesafe and can be extended
by adding methods or fields
Arrays
In C# an array corresponds to an object of the Array class
Java each array is a direct subclass of the Object class
Both support arrays of arrays (jagged arrays).
C# also has true multidimensional arrays Increase performance because of
increased locality
Partial Classes
Enables one to define a single class, struct or interface across multiple source files Useful when dealing with automatically
generated code Automatically generated parts of a class
can live in one source file while the user generated parts of the class can live in another
Java has no such corresponding concept
C# concepts with no Java counterpart…
Verbatim Strings Overflow Detection Explicit Interface Implementation Friend Assemblies The Namespace Qualifier Iterators (Continuations) Static Classes Nullable Types Anonymous Methods
…Java concepts with no C# counterpart
Extensions strictfp Dynamic Class Loading Interfaces That Contain Fields Anonymous Inner Classes Static Imports
Performance
The results are in MFlops (mega floating-point operations per second), so higher is better.
More Information
For a far more in-depth comparison check out:
http://www.25hoursaday.com/CsharpVsJava.html#structs