Comparing Java with C#
I started programming in Java a few days ago and immediately felt in love with Java. As a person who have used C# for seven years, I was afraid that I wouldn't appreciate Java, would find that a lot of C# features are missing in Java, and that in general, Java feels like old, or better to say legacy variant of C# full of feature creep.
Still, I had to start using Java.
Why using Java in the first place?
Since the shift to Linux eight months ago, most software had to be rewritten, because most software was written in C#. This led to an obvious (at least for me) choice of Python and Node.js—first because of its elegance and ease of use, second because of its unique event-based model and my relative familiarity with JavaScript.
Since practically every piece of software was either a web application or easily movable to web, the migration went fine, with one exception: our in-house passwords manager—a desktop application which keeps all my passwords for all websites. While I already thought about moving it to web, the idea that my passwords will be on a server is very scary. There is no objective reason (there is more risk to get my desktop PC hacked than the servers), but I don't care. It's scary, that's all.
This meant that I have to make a choice between C# with Mono, Python, C++, Java or something else (including some esoteric choices such as Haskell).
C# on Linux feels wrong. Not that I'm against Mono, but C# on Windows is The Right Choice™ because it is a de facto standard, especially with Visual Studio in play. On Linux, the support of C# is not as good, there is no decent IDE (if compared to the excellent Visual Studio), and it's not a de facto standard choice of Linux developers. The only reason to use C# was that “this is what I know the best”—a reason I find completely stupid.
Python's support of desktop applications looks suspicious too. While Python's community encourages the usage of Python outside the classical web applications on Linux, I'm unhappy to pick Python for this job either. It looks like, once again, the only reason to use it is because “this is what I already know”. So what about using this opportunity to learn a different language?
C++ looks like an excellent candidate. It's a language I should learn anyway (why? because it's useful, because it contains many concepts which help understanding what happens under the hood in other languages and finally because any developer should know C++). The problem with C++ is that it's complicated and I can easily get it wrong. Given that the password manager should be the piece of software I can trust and rely on, choosing a language in which I can easily screw things up is not an option.
Esoteric choices. I seriously considered Haskell for this job, but the learning curve is too high. Again, the risk of ending with a piece of software which is not as reliable as its C#'s counterpart is not an option.
Java appears in this context to be a great choice.
Java rocks!
From the first day, I felt in love with Java. While it lacks a few features, it is by no means inferior or more legacy than C#. The fact is, it seems to have much more features, without having feature creep.
The power of Enum
s is impressive. It already helped considerably when rewriting the password manager: Java code is much easier and more readable, and things which in C# had to be in two locations are now within the Enum
itself. If I would have to use C#, I would really miss the power of Java's Enum
s.
Java also has a few language constructs which I lacked in C# for years. For instance, I always needed the ability to declare a variable as final
so that it is declared only once and then never changed. C# doesn't have anything like that (readonly
for fields doesn't count). Java does have final
. When I look at my code, practically every variable and every parameter is final
. Less bugs; thank you, Java.
Finally, I find Java API more consistent with other languages. .NET Framework has LINQ; great. The problem with LINQ is the choice of the names: Where()
instead of filter()
or Select()
instead of map()
.
This makes .NET Framework very counter-intuitive for developers who used other languages before. You want to limit a stream of values to 10 values, but there is no Limit()
: you have to know that you should use Take()
instead. You want to use an accumulator function, but there is no Reduce()
or Collect()
, but Aggregate()
.
Interestingly enough, I've read many articles in the past which were explaining why C# is so superior to Java. But they were probably comparing new versions of C# with very old versions of Java. The major benefit of C# was LINQ. Java 8 introduced lambda expressions, and its actual API is by far more consistent with other languages, that is easier to use. In most other aspects, Java outperforms C# in my case, since the produced Java code is more elegant, shorter and safer than its C# counterpart.
The ecosystem looks very pleasant as well. Eclipse 4.4 with Java 8 works very well on Ubuntu. More importantly, it provides a lot of features by default, including the auto-formatting tool (much more powerful than the basic formatting available in Visual Studio), the severity level of the compiler (which is similar to Microsoft's Code analysis, but more integrated within the development environment) and the style checker, well integrated in the IDE as well.
Java has its weak points. The lack of properties, for instance, is quite annoying. This being said, it is a great language which is by no means inferior to C#.