perlun.eu.org · About the blog · Programming · Select language / Välj språk

Java for C# programmers

In this post I'll write about a number of differences between Java and C# that I've experienced at my current job. First a bit of a background: Before I switched jobs in August 2018, I had been working with C#, Ruby and other programming languages for the last 15 years; Java was pretty much out of the picture for me. I had worked as a Java programmer from the year 2000 to 2001, but this was indeed a long time ago.

But then, something happened. As the saying goes, "the Lord works in mysterious ways" - this was really the case for me this time. I wrote in a blog post a few years ago about my feelings for the Java programming language; they weren't exactly ecstatic:

Java lacks a lot of the nice things that has happened in C# for the last ten years that improve developer ergonomics and code readability. Yes, it's slowly improving, but the improvement process is dead slow. Let this VM die a painful death and rest in pieces. :laughing: Well, to be honest, the VM is quite OK, it's the Java language that really deserves to be replaced with better alternatives.

Little did I know back then I would be here, pretty much exactly two years later, working full-time as a Java programmer. Sometimes, life makes you surprised. (Or, put another way, you can say that the Lord makes you surprised sometimes.)

Have I changed my mind, or was I wrong that time, two years ago? Or both?

Some things have indeed happened since that statement. The improvement process has been significantly revamped; instead of releasing a new major version every 2-3 years, Java has moved to a 6-month release cycle. Improvements are slowly but steadily making it into the language, and in some areas, Java is even ahead of C# which is quite impressive, given Java's historically lower velocity. And, I'm quite certain that both of these language camps (or rather, development platform teams) are monitoring the steps of each other, trying to learn from the experiences at the other side of the pond instead of necessarily reinventing everything over and over again.

Having that said, there are significant differences between these languages and during my current 6 months of working more or less full-time with Java again, I've tried to jot down a few of these every now and then. This is helpful both for myself and also for other people who are crossing the "border", particularly in the C# -> Java direction. Having worked a long time with a language tends to make you think in a particular way, tends to make you expect certain characteristics of a language and platform. These presumptions can be dangerous, since they are very likely to be incorrect from time to time.

So, here is my list. Bear in mind that this is written from a C# programmer's perspective; it primarily describes ways in which Java is different from C#, not the other way around.

(This is to be considered a "living" document and I'll likely continue updating this as my journey goes on.)

Differences between Java and C#

Identifier visibility

(See also this SO question)

Language runtime and language version much more tightly coupled

ClassLoader

Debugging

Static constructors

Static classes

Static nested classes

Interfaces

Enums

Generics

Primitives don’t have methods

switch(obj) throws exception on null references

Do or do not; there is no try

No delegates