Comparing Java with C#

Arseni Mourzenko
Founder and lead developer
177
articles
March 19, 2015
Tags: java 2 short 50

I start­ed pro­gram­ming in Java a few days ago and im­me­di­ate­ly felt in love with Java. As a per­son who have used C# for sev­en years, I was afraid that I wouldn't ap­pre­ci­ate Java, would find that a lot of C# fea­tures are miss­ing in Java, and that in gen­er­al, Java feels like old, or bet­ter to say lega­cy vari­ant of C# full of fea­ture creep.

Still, I had to start us­ing Java.

Why us­ing Java in the first place?

Since the shift to Lin­ux eight months ago, most soft­ware had to be rewrit­ten, be­cause most soft­ware was writ­ten in C#. This led to an ob­vi­ous (at least for me) choice of Python and Node.js—first be­cause of its el­e­gance and ease of use, sec­ond be­cause of its unique event-based mod­el and my rel­a­tive fa­mil­iar­i­ty with JavaScript.

Since prac­ti­cal­ly every piece of soft­ware was ei­ther a web ap­pli­ca­tion or eas­i­ly mov­able to web, the mi­gra­tion went fine, with one ex­cep­tion: our in-house pass­words man­ag­er—a desk­top ap­pli­ca­tion which keeps all my pass­words for all web­sites. While I al­ready thought about mov­ing it to web, the idea that my pass­words will be on a serv­er is very scary. There is no ob­jec­tive rea­son (there is more risk to get my desk­top 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 be­tween C# with Mono, Python, C++, Java or some­thing else (in­clud­ing some es­o­teric choic­es such as Haskell).

Java rocks!

From the first day, I felt in love with Java. While it lacks a few fea­tures, it is by no means in­fe­ri­or or more lega­cy than C#. The fact is, it seems to have much more fea­tures, with­out hav­ing fea­ture creep.

The pow­er of Enums is im­pres­sive. It al­ready helped con­sid­er­ably when rewrit­ing the pass­word man­ag­er: Java code is much eas­i­er and more read­able, and things which in C# had to be in two lo­ca­tions are now with­in the Enum it­self. If I would have to use C#, I would re­al­ly miss the pow­er of Java's Enums.

Java also has a few lan­guage con­structs which I lacked in C# for years. For in­stance, I al­ways need­ed the abil­i­ty to de­clare a vari­able as final so that it is de­clared only once and then nev­er changed. C# doesn't have any­thing like that (readonly for fields doesn't count). Java does have final. When I look at my code, prac­ti­cal­ly every vari­able and every pa­ra­me­ter is final. Less bugs; thank you, Java.

Fi­nal­ly, I find Java API more con­sis­tent with oth­er lan­guages. .NET Frame­work has LINQ; great. The prob­lem with LINQ is the choice of the names: Where() in­stead of filter() or Select() in­stead of map().

This makes .NET Frame­work very counter-in­tu­itive for de­vel­op­ers who used oth­er lan­guages be­fore. You want to lim­it a stream of val­ues to 10 val­ues, but there is no Limit(): you have to know that you should use Take() in­stead. You want to use an ac­cu­mu­la­tor func­tion, but there is no Reduce() or Collect(), but Aggregate().

In­ter­est­ing­ly enough, I've read many ar­ti­cles in the past which were ex­plain­ing why C# is so su­pe­ri­or to Java. But they were prob­a­bly com­par­ing new ver­sions of C# with very old ver­sions of Java. The ma­jor ben­e­fit of C# was LINQ. Java 8 in­tro­duced lamb­da ex­pres­sions, and its ac­tu­al API is by far more con­sis­tent with oth­er lan­guages, that is eas­i­er to use. In most oth­er as­pects, Java out­per­forms C# in my case, since the pro­duced Java code is more el­e­gant, short­er and safer than its C# coun­ter­part.

The ecosys­tem looks very pleas­ant as well. Eclipse 4.4 with Java 8 works very well on Ubun­tu. More im­por­tant­ly, it pro­vides a lot of fea­tures by de­fault, in­clud­ing the auto-for­mat­ting tool (much more pow­er­ful than the ba­sic for­mat­ting avail­able in Vi­su­al Stu­dio), the sever­i­ty lev­el of the com­pil­er (which is sim­i­lar to Mi­crosoft's Code analy­sis, but more in­te­grat­ed with­in the de­vel­op­ment en­vi­ron­ment) and the style check­er, well in­te­grat­ed in the IDE as well.

Java has its weak points. The lack of prop­er­ties, for in­stance, is quite an­noy­ing. This be­ing said, it is a great lan­guage which is by no means in­fe­ri­or to C#.