Any comparison of C# and Java's language features will always be contentious. The parable about the three blind men describing an elephant illustrates the issue well --  no one developer has used or experienced all the features of the current C# or Java versions. I certainly haven't. This comparison focuses on the ways Java 7 and C# 5 differ. Java 8 is scheduled to become generally available in March 2014 and C# 6.0 will likely appear sometime in 2014 as well, but for now let’s focus on the current versions. Java vs. C#Both languages are tools and pretty similar tools at that, a bit like comparing a Phillips head screwdriver and a flat head screwdriver. Here are some of the ways they differ.


On the surface, C# and Java use the same generic syntax but they actually handle generics very differently. First off, the names are different. For example, a List<T> in C# would be an ArrayList<T> in Java. (ArrayList in C# is 1.0/1.1 and non-generic). Both generic types prevent you from putting the wrong type in the list, but they have different methods of doing it: C# generates code when the class is loaded at runtime, while Java does type erasure. Java's method loses the runtime type information, so it needs to cast. The compiler inserts those run-time casts which hurts performance. Java has a little extra flexibility with wildcard generic parameters for methods, which you have to finagle with interfaces in C#. But in C#, generic support is part of the intermediate language (lL), so there's just one set of code created for reference types. Overall, C# tends handle generics better, with faster execution (no boxing with value types). For a deeper look at the differences between their methods, check out this article by Jonathan Pryor.

Unified Type System

This is another one slightly in favor of C#. In Java, the primitive data types are byte, short, int, long, char, boolean, float and double, and they're the most basic types. In C#, all primitive data types (int, float) are objects. This means that an int in C# literally inherits all the methods of object. It's important to realize, however, that the primitive types are still value types. Boxing (the conversion of a value type to a reference type) occurs in both C# and Java. [caption id="attachment_108931" align="aligncenter" width="260"] Data Types Example Code C# inherits all the methods of object.[/caption] Additionally, C# supports unsigned types byte, ushort, uint and ulong but there are no unsigned types in Java. In Java, the long type is needed for integers between 2 billion and 4 billion whereas a uint will suffice in C#. One area where Java had the upper hand was with BigInteger. It's had BigInteger for precision integers for many years and C# didn't have it. But C# caught up with its own BigInteger type in .NET 4. (When I say C#, I mean .NET -- this is also available to VB.NET and other .NET languages). It hasn't, however, added Java's BigDecimal.

Checked Exceptions

C# only supports unchecked exceptions but Java has both checked and unchecked. A checked exception is one specified in the method signature. The method doesn't handle it but expects a caller to. The C# position on exceptions is if a particular section of code could produce one, wrap it in a try { particular_code;} catch { /* do something here*/} with an optional finally clause. For unchecked error handling, Java is the same. Be aware, however, that although allowed in Java, return statements inside a finally clause are not a good idea. The example below, for instance, always outputs 2. Exceptions Sample Code @SuppressWarnings is included to remove warnings about the finally clause. C# avoids this by banning returns in a finally clause. Oracle sums up the Java position as "If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception." Java 7 has added a new feature for handling exceptions, try-with-resources: Try-with-resources sample code It's a lot like the using statement in C#, which automatically disposes of resources implicitly, but the Java version can have catch and finally clauses. Putting try, catch, and finally inside a C# using clause is possible but always looks a bit inelegant. The Java version is better. This is by no means a complete list of language differences, just a few key ones. I'll add a few more to the list in another post next month.