Contrasting C# and Java Syntaxby Raffi Krikorian
In the first article of this series, I translated a trivial Java program into C# and pointed out the differences between the two implementations. This time through, we are going to focus specifically on the syntax of C#. What I am going to do is point out when the syntactical structures are the same, and spend time talking about when they are different. There are a few syntactical "extras" in C# that do not have equivalents in Java, and I am going to spend time pointing those out as we go along. Let us start small and then go big.
Primitive and Simple Types
Java has a couple of primitive types that everybody is familiar with: byte, char, int, long, float, double. Primitives are the basic building blocks of Java; they are the smallest "unit." What usually annoys annoys most programmers is that primitives are disjoint from the object model of Java; whereas all objects in Java extend from
java.lang.Object, primitives do not extend from anything. This means that any class that operates on objects (i.e., objects in the Collections API) will not work with primitives. The primitives must be mapped into the object model in order to use it.
This is not the case in C#. C# uses the .NET object/type system so that C# programs may inter-communicate with other .NET languages without having type confusion -- for example the, type int is an alias to
System.Int32 which in turn ultimately extends from
System.Object. This means that the primitive, or simple, types in C# function just like any other object. For example, it is valid to call the C#
GetType method on any simple type.
|Table 1: Primitive Types in Java and C#
A mapping from the familiar Java primitive types to their C# simple type equivalents (and the objects they actually alias from).
Even though the C# simple types are technically objects, they are still passed by value just like their Java counterparts. (Java primitives are passed by value and Java objects are passed by reference.) This is the case because aside from being objects, the C# simple types are all structs -- objects that are passed by value as opposed to being passed to reference (we will get to these further on).
What are your thoughts on the learning curve involved with learning C# syntax as a Java or C++ programmer?
Now that we have primitive data structures, we should start organizing them into higher level statements. You will note that there is not much to say to Java programmers as they are already familiar with most of these syntaxes; we will focus on the differences between Java and C#.
Variables are defined in C# just like they are in Java:
int integer = 3; bool boolean = true;
Java uses the "static final" keywords to make a variable constant; in Java a "static final" variable is class-scoped instead of being object-scoped, and the compiler prevents any other object from changing the value of the variable. C#, in turn, has two ways of declaring a variable constant.
Marking a variable
const causes the value to be inlined before compiling. With the definition
const int TWO = 2;
2 * TWO
is converted to
2 * 2
by the preprocessor before compilation. This causes the compiled program to run faster as it does not have to look up the value of the constant during run-time.
Considering that constants are often used for things like
TIMEOUT, it may be desirable not to have this inlined into the code; if this field is marked
const, then any code compiled against it will inline the constant and will need to be recompiled in order for it to change. If the constant is instead marked with
readonly, then the next time the application is executed the behavior will change as the code will check the value of the
readonly field, while the compiler still protects it from being programmatically changed.