16
A Comparison of Java and C# Peter Juszczyk CS 492/493 - ISGS

Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

Embed Size (px)

Citation preview

Page 1: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

A Comparison of Java and C#

Peter JuszczykCS 492/493 - ISGS

Page 2: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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.

Page 3: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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".

Page 4: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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

Page 5: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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

Page 6: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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

Page 7: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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

Page 8: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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

Page 9: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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

Page 10: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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

Page 11: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

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

Page 12: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

…Java concepts with no C# counterpart

Extensions strictfp Dynamic Class Loading Interfaces That Contain Fields Anonymous Inner Classes Static Imports

Page 13: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

Performance

The results are in MFlops (mega floating-point operations per second), so higher is better.

Page 14: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Page 15: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Page 16: Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#

More Information

For a far more in-depth comparison check out:

http://www.25hoursaday.com/CsharpVsJava.html#structs