Home > Articles > Programming > C#

Why C# Is Not Java

  • Print
  • + Share This
C# and Java aren't the same thing-or even all that similar. Joe Mayo peeks under the hood to show why C# is its own language.
From the author of

From the author of

From day one, when Microsoft officially announced C#, on June 26, 2000, Java zealots have been preaching mantras that C# is a rip-off of Java or that C# looks just like Java. Although there are similarities derived from their common C and C++ heritage, characterizations such as these are less than accurate. Having been quite fond of Java myself, I must admit that it was a great language. It filled a void in Internet software development that other languages left open during the late 1990s and early 2000s. However, we now have a new choice with C#, which has its own signature and style and which is definitely not the same as Java.

C# introduces several modern language elements that Java doesn't even have. Furthermore, a peek under the hood will reveal semantics that differentiate C# from any other language. This article explores each of these areas, supporting the perspective that C# is its own language.

Types

C# has unsigned integral types (byte, ushort, uint, ulong). Furthermore, C# has a decimal type that holds a floating-point number and is used primarily for financial calculations. Now, if a C# program uses these types, how can that look like Java, which doesn't have any unsigned types or a decimal? Sure, Java has a byte type, but it's signed, whereas the C# sbyte (with the letter s in front) is signed and the two don't look alike. Some of you may be thinking that it would be just as easy to use a Java double for financial calculations. However, with 28 digits of accuracy, the decimal type is not the same as a double.

Converting primitive types to objects is also quite different. Here's a comparison:

In Java:

   int Bar = 13;
   Integer myObject = new Integer(Bar);
   int Foo = myObject.intValue();

In C#:

   int Bar = 7;
   Object myObject = Bar;
   int Foo = (int)myObject;

Why would anyone think that this looks the same? Java takes a value of an int and adds it to a specialized object. Then it calls a special method to extract the value from the object.

C# doesn't have to create special objects to obtain reference semantics for its primitive types. It employs a concept of type system unification in which all types are objects. The conversion happens implicitly with a simple assignment, without needing a special object crafted just for that type.

Another type that Java doesn't have is the enum. Here's a declaration and typical usage of a C# enum:

enum NoMonkey { See, Hear, Do };
. . .
NoMonkey foo = NoMonkey.Do;

Although Java can add static final variables to interfaces, they don't have much other functionality than their values. In contrast, C# can convert enums between their strongly typed enum value, their underlying integral type, or a string representation of the enum itself.

Java doesn't have verbatim string literals to enhance readability of certain strings such as the one shown in the following C# statement:

string bar = @"c:\My Documents\bar.txt";
  • + Share This
  • 🔖 Save To Your Account