Want to show your appreciation?
Please a cup of tea.

Monday, May 25, 2009

Random Thoughts About Java vs. C#/.Net

There is no intention to give an extensive comparison. I just want to log the things that I found worth recording during my two tests.

Monitor performance - winner: .Net

Please read those posts above. The winner is clearly .Net, Java was close to .Net in single thread test but getting worse and worse in the tests with more threads and more physical CPUs. We need a better JVM!

Closure or Delegate MS named it - winner: .Net

Suffered from the lacking of the Closure, I had to write an additional method getDescription and used two switch statement to achieve what I did with one delegate field in .Net. I know a lot of Java people don't like Closure because they want to keep the simplicity of Java. But come on, all junior C# developers are using Delegate happily. It is time to improve the language features so I can write more effectively in Java.

Concurrent library - winner: Java

I can easily use CyclicBarrier and CountDownLatch in Java to achieve thread synchronization. But I had to write my own Barrier and wait for thread to end instead of task complete in .Net. In the case of Java, the code can be easily changed to thread pool. But in order to do the same in .Net, I'll need to write my own count down latch.

BTW, at this moment, Java has way better libraries, both build in and 3rd party (including open source) then .Net from all aspects. Concurrent library is just an example. Most of things from MS P&P are inferior then the counterpart of Java and open source .Net world. Almost every MS library offering is invasive because they want to lock you in.

Synchronized keyword - winner: Java

I know there are a lot of debate on this topic. I agree that Java's synchronized keyword as method modifier exposes internal synchronization detail externally and other code can lock the same instance to interfere and causes dead lock. But:

  • For my internal/private scoped class, it is so much easier to write in Java comparing to .Net. Take a look at the same functional class Accumulator in Java and Accumulator in C# and tell for yourself. (You may have noticed that I missed the lock in Average property getter and had to fix in a later revision. Tedious!).
  • OK, locking on the object itself is bad, but why cannot C# automatically define a field and use that to all parameterless lock and have the synchronized (or locked if MS had to given everything a different name) back as a valid modifier for members?

IDE - winner: Java

Well, I'm talking about Eclipse. I know Visual Studio had improved a lot, but hey, in Eclipse, my local variables are black, and fields are blue and static fields are italic. In Visual Studio, all are black. The readability is horrible if I don't have a good naming convention. There are more and more to mention but I'll leave it for others to discover.

Generics - winner: None

See: How Good Is .Net Generics

Final thoughts

  • Java is getting further behind in the VM performance and language feature. As C# continues to improve from version to version, Java really need to catch up.
  • .Net fall far behind from the library perspective. The design of build-in libraries are nowhere close to Java's offering. With Java, I can quickly assemble an enterprise application from readily available high quality libraries so I can just focus on business logic. But from project to project on C#.Net, I had to write a lot of non business code and from time to time workaround bad implementations inside the .Net build-in classes.

Time will tell who will win the race eventually.

2 comments:

Anonymous said...

Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.angularjs best training center in chennai | angularjs training in velachery | angularjs training in chennai | angular 4 training in chennai

Snehal Harshe said...

I am glad to see this brilliant post. all the details are very helpful and good for us, keep up to good work.I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
Java Course in Bangalore

Post a Comment