C# or Java?
There's a lot of intolerance around the question of which language is best. Really it depends (although I do have a personal, rationally arrived at preference). The following pages explore some of the similarities and differences between Java and C#, with a view to helping you decide. This kind of discussion is notoriously difficult to keep current, so if you see any errors, please let me know. At the time of writing C# is at version 5.0, .NET is at version 4.5, and Java has just updated to version 1.8 (what marketing refer to as Java 8).
First, let's look at the core similarities. Both languages are class-based, object-oriented, and built on top of a virtual machine (VM) with automated memory management. In the case of Java, the VM is called the Java Virtual Machine (JVM) and the language is supported by an extensive collection of utilities in the Java class libraries. In the case of C#, the virtual machine is called the CommonLanguage Runtime (CLR). This forms part of the broader .NET framework, which serves a similar role to the class libraries of Java.
Although both languages offer essentially the same features, and have very similar rationales, there are some subtle (and not so subtle) differences that might be significant to you and your project. The following list of differences will gradually be expanded with some more detailed evaluations:
- Syntactic Sugar
- Properties (C# has them, Java doesn't)
- Implementing an interface vs extending a class (Java distinguishes these cases syntactically, C# doesn't)
- Core Language Features
- Unsigned intergral types (C# has them, Java doesn't)
- Full runtime support for generic types (C# has it, Java doesn't)
- Operator overloading (C# has this, Java doesn't)
- Definition of the protected access modifier (Java's is broken)
- Multi-dimensional arrays (C# offers jagged and rectangular arrays, Java only has jagged arrays)
- Partial types (C# has them, Java doesn't)
- Pass by reference semantics (C# has it, Java doesn't. No, it really doesn't!)
- Checked and unchecked exceptions (Java has both, C# only has unchecked exceptions)
- Default implementation in interface definitions (Java has this, C# doesn't)
- Nested classes (Java has 2 types of inner class, C# only has 1)
- Implementation of enum (C#'s is the same as C and C++, Java's is fully object-oriented)
- Struct (C# has this, allowing user-defined value types, Java doesn't)
- Nullable value types (C# has this, Java doesn't)
- Toolset Choices
- Source documentation facilities (Java comes with JavaDoc as standard, C# needs a 3rd party solution)
- Java has a way better range of media handling libraries, C# requires the use of 3rd party code (apparently this will change with the version of .NET in Windows 10).
- Java has a choice of IDEs: Eclipse and NetBeans are the two main options. C# really only has Visual Studio. Although there are free versions of Visual Studio, Eclipse and NetBeans are both completely free.
- Java makes the source code for the libraries available, C# does not.
With my computer science hat on, the killer difference is the runtime support for generic types in C#. This makes writing and using generic types in C# easier and lets me create generic arrays, which is impossible in Java. Given both Java and C# make a big deal about having runtime management, having a major language feature that is not actually supported in the runtime just seems wrong. Operator overloading is also an important feature that many languages support, used sparingly it's a really useful technique - it's a shame Java doesn't have this. I also like the inclusion of proper multi-dimensional rectangular arrays in C#. This is a nice conceptual addition, although it's by no means critical.
On the flip side, I miss JavaDoc. The tools available for C# are just not as neat. Also, Java's inner classes are more flexible than the equivalent mechanism in C#. Effectively, they support a kind of closure for the inner class with respect to the containing class. And Java's object-oriented implementation of enums is pretty neat too.
From an academic perspective, the really big win for Java is that all of the source for the libraries is available. This is great learning resource as you can go and read for yourself how Java implements that generic linked list utility.
And on the fence, I'm still trying to decide whether I miss checked exceptions or not...
If I'm honest, there's something about the original Java Community Process and the willingness of Sun to put significant parts of the technology under various forms of GPL that sit better with me than the typical closed source approach of Microsoft. However, I'm not convinced about Oracle's whole-hearted embrace of that ethos since buying Sun. And anyway, that's a political argument, rather than a technical one.
At the time of writing, Java is considerably more popular than C# (according to the TIOBE Index). But I wouldn't let that sway you too much: my favourite language Prolog scrapes in at number 33 with a meagre 0.48% share. That doesn't mean it's not a brilliant language in its own right.
While we're talking numbers, what is probably the most significant trend is the popularity of Objective-C, currently at number 6 (and falling) with a 3% share. Ten years ago this language was at number 47, but last year it was the number 3 language! Why the change? This was the only language you could really use if you wanted to program for iOS devices, this took it to the top of the charts. However, the introduction of Swift as the iOS language of choice has cut into its share.
So, individual language wax and wane in popularity. But there are two more general features of the language chart that are deeply significant. Firstly, all but 2 of the top 10 languages include some degree of object-orientation, and five of them are fully object-oriented. This shows how dominant the OOP paradigm has become. Secondly, five of the top 10 languages have a syntax derived directly from the language C, and C itself regularly trades places with Java for the top spot. This shows the dominance of this basic way of writing code. Both of these facts strongly suggest that learning an object-oriented language based on the C syntax is going to be a useful skill.
But let's not forget these numbers are controversial. PYPL has a different top 10 languages.