I will admit it – the last time I did any serious Java programming Bill Clinton had just started his second term and Princess Diana was still alive. I kept up with the news here and there, for example I investigated (and quickly dismissed ) Java’s generic support back in 2005. Also my friend Llewellyn Falco had several Monday night code sessions that I attended which used Java. But on the whole my Java knowledge was … ahem… a bit stale. But all of that was about to change because I need to write a cloud course which covered a number of platforms, one of them being: Java.

As part of this little exercise, I am going to blog about some of the differences between the C# and Java languages. I am not just talking about how the C# readonly keyword is the same as the Java final keyword, I am talking about the major differences liked generics, checked exceptions, delegate support, properties, etc.

I jumped back in by flipping through the Java tutorial. The fact that a fully sponsored and standardized tutorial exists is already a point in Java’s favor. Not that there aren’t C# tutorials, there are in fact 3.6 quadrillion of them (rough guess). I even found one from Microsoft but it was for .NET 1.1 and a lot has changed since then.

After a while of quickly flipping through the tutorial I thought I had something that was notably different – namely explicit constructor initialization. I was thinking that the Java version could be more powerful that the C# version, until I read the line:”If present, the invocation of another constructor must be the first line in the constructor.” Crap. Never mind, with that limitation it is essentially the same as C#.

OK, a few more pages and I came across static and instance initializer blocks. However, once again after reading more closely it seemed like the recommended approach was to use private static methods and protected final methods to do the same thing (which is the approach C# has to take). So for those of you keeping score:

Boring 2, Interesting 0.

The next item I came to was on the subject of nested classes. I will confess I rarely use nested classes in C#, and I have a feeling that most developers in Java would say the same thing. When I do use them it is because I have classes that are tied to the state of the parent object. A great example of this is the state pattern, where you have an object that can transition between many states and the behavior of the object changes when the state is changed. In cases like that rather than allowing outsiders to change the state to whatever they want, you can make the states inner classes and only they can set the state.

This wikipedia article on the comparison of C# and Java is somewhat misleading in this regard. If you search for “Instance level inner classes”, you will see it states that Java supports both class level (static) and instance level nested classes, whereas C# only supports class level. However, C# *does* support both class and instance level classes but it does so in a way that is fundamentally different than Java. What Java has that C# doesn’t have is a concept of the outer this pointer within the inner class. That is what the wikipedia article is referring to. This means in C# you have to pass the outer object instance around just like you would with other classes, whereas in Java you get that for free.

A couple of pages later there was another interesting tidbit on local and anonymous inner classes. However, the tutorial writers were apparently just whetting my appetite, because they didn’t go into any detail at all. I am sure I will come back to that later.

Moving on. Finally the first significant difference between .NET and Java: enums. So major in fact that I had to move it to its own separate post.

No comments yet.

No Comments

Sorry, the comment form is closed at this time.