NOTE: If you don’t want to read my novel here, just scroll down to the “Final Verdict” section near the bottom.
-=-=-=-
First off, I've worked on major systems in both Java and .NET. I've even done some work for MS themselves (did some programming on their web site using C#). My own company's web site is developed in ASP.NET and our chat server software is developed in Java. Additionally, I'm a Sun-certified Java programmer and will have my MS programming certification soon.
I realize this is a long post; I’ve tried to cover all areas in here though so I feel it’s relatively complete. I also know that my opinion is going to be unpopular based on the previous answers. To be honest, this is a bit of a surprise; most public forums generally involve MS products being soundly bashed.
There are a few things you must look at when comparing Java to C#; specifically the core language itself, the framework for the language, the development environment, and the community as a whole.
To explain this C# is just a language. .NET provides a framework/runtime and core libraries that can be utilized by C# code. The Java analogy is the Java language and the J2SE and J2EE libraries as well as the JVM runtime. We could compare runtimes separately, but no real need for this discussion.
Language - C# vs. Java
C# has a few things missing in Java including operator overloading, delegates, unsafe-code blocks, properties, etc. Specifically, I personally like it's use of delegates to clean up event handling and unsafe blocks for some high-performance operations. There are other differences as well, but they get a bit esoteric for this level of discussion.
With that said, C# provides a lot more rope with which to hang yourself. Java intentionally left out operator overloading and a few of the abilities C# has included. These were determined to cause code to be both more difficult to maintain and harder to debug. In this case, I have to agree with Sun. Operator overloading in particular is heavily abused and can dramatically obscure the meaning of a given piece of code. Additionally C# doesn’t support throwing checked exceptions, only unchecked. I feel this was a terrible omission as it makes it difficult to properly handle exceptions in deep call stacks.
FYI: I suspect much of the reason MS kept some of “risky” C++ abilities around was to convince their VC++ developers to migrate to C#. I believe they have had only limited success with this migration though.
Verdict: C# by a nose but be careful. You can easily get in over your head with some of the more “risky” operations that it allows. Choose Java if you want “safer” code and less difficult debugging.
Framework/Runtime – .NET vs. J2SE/J2EE
The framework contains the entire set of core APIs that are accessible to the language when it’s running. These are what let the language actually DO something like loading files, displaying an image, showing a button and handling when it’s clicked, etc.
Simply put, J2SE/J2EE trumps .NET by a huge margin. There are a few APIs in .NET that are very well done, specifically I like .NET’s handling of asynchronous I/O as simply callbacks on the standard I/O objects. That’s very clean. I also feel that ASP.NET is superior to basic JSP/Servlets. I feel that .NET’s Winform APIs are pretty nice too. While providing less widgets then Swing (Java’s primary GUI code) they are a little easier to use and run a bit faster.
With that said, there are huge deficiencies in .NET primarily relating to “big” systems. I also feel .NET’s APIs contain some real bonehead designs. For instance, .NET provides a very elegant way to load XML configuration files. Except you can’t specify the file you want it to use! It will only auto-discover the file, no support for specifying it directly. That means if you want to use multiple configuration files, you need to write your own code that works like .NET’s already. This is a very poor decision on MS’s part and shows their lack of forethought in the API’s design.
Another example of poor design is that Microsoft “borrowed” the design of the JDBC (Java’s database libraries) from Sun. But they neglected to properly implement the concept of a DriverManager. This means that most people’s code that communicates with a database is tied to a specific database as opposed to database-neutral (which the API allows if you create your own DriverManager-style class). This is very irritating and sloppy on both MS’s part as well as the users. Java’s API design is generally much more open and extensible all around.
There are also major aspects of Java that are vastly superior then (or no direct comparison to) .NET: JAAS security model to provide your own application security for anything, EJB (hugely scalable and distributable business components as opposed to the rather limited-in-comparison COM+), JMS vs. MSMQ, integrated connector architecture as opposed to requiring BizTalk, JNDI vs. Active Directory as a way to lookup objects (the irony is that Java can talk to Active Directory beautifully too), CORBA (totally missing in .NET), etc. When you look closely, you will find that most APIs in .NET are missing a few things provided in Java and there are very few the other way around.
Simply put, .NET has a nice core library, but as you build more sophisticated and powerful applications you start feeling the pinch. Microsoft has attempted to use .NET to leverage their platform offerings directly while Sun has simply tried to make Java support everything. This difference starts to show on larger apps. Java has dramatically superior per-machine scalability as well because it isn’t limited to Microsoft infrastructure (i.e. it can run on 64-processor machines just as easily as my mother’s Pentium 2). Both of them are fine for small applications but I generally pick Java due to the open source offerings (I will cover that below).
From a runtime standpoint the key differences are that the JVM (Java runtime) runs on many machines while the CLR (.NET runtime) runs on one machine but supports many languages. Yes, I know about the Mono project, but it isn’t complete and isn’t supported by Microsoft so I’m leaving it out. I originally felt the many-language support would be a useful thing, but I’ve learned that it’s not particularly common and most companies do either VB.net (yuk) or C#. The many machine advantage is huge though. It prevents vendor-lock-in (something that companies using Microsoft can often come to regret) as well as making development easier. I often develop and test on Windows desktops and then run the code on huge Solaris or mid-sized Linux machines. “Write once, run anywhere” is the motto of Java and contrary to any naysayer it’s completely true.
From a performance standpoint, it’s a wash. .NET gives you access to DirectX, making it a clear winner for games. Java absolutely crushes .NET’s socket performance. There are many tit-for-tats in the performance arena so you can’t really decide on that alone. In general, .NET will outperform Java on Windows machines. This is almost always the case for GUI apps. When it comes to web applications, it’s going to depend more on how you wrote it then anything else. I will say though that the edge goes to Java for one overriding reason: platform independence. Wintel boxes are not great for cost-effectiveness. I can simply install a free Solaris or Linux OS on that same hardware and get an immediate and LARGE performance increase without changing a line of code; the win has to go to Java.
Verdict: Java by a landslide. With the exception of a few Windows-specific abilities and a few odds and ends, almost nothing is available in .NET that you can’t do in J2SE and J2EE (and generally do it better). Anything that IS missing will be available via open source (and often more capable – i.e. Microsoft’s SQL Server drivers vs. JTDS).
Development Environment – Visual Studio vs. Java IDEs
This is a small comparison. Anyone doing anything with .NET will be using Visual Studio for the most part. On the Java side of things, you are open to many options; personally I use NetBeans. Many developers use Eclipse. There are many other IDEs for Java as well.
Overall, I think Visual Studio is a fine IDE but about 3 years behind any of the latest Java IDEs. Features like full refactoring support (want to rename a method/class/variable? Just select it, rename it, and the IDE will make all needed changes across the entire project, useful beyond words); one-button integration with application servers, built-in performance profiling, significantly better editor in general (bookmarks, code-completion with the source documentation included, etc).
Additionally, unless you resort to using something like NANT (build tool for .NET based on the Java build tool ANT) then you are stuck with the Visual Studio build environment. This is very primitive and clumsy. Many organizations simply give up on it and use NANT or ANT directly. Those that don’t are plagued with storing their VS.NET projects in version control and forever fighting issues that crop up on specific developers machines and not others. Configuring my machine to compile an application shouldn’t be more complicated then the application code itself yet in .NET I’ve seen this be the case more often then not and all due to VS.Net’s build model. Very poor design MS!
Version control integration is better in the Java IDEs as well. They support many different version control tools as opposed to Visual Studios support for the horrid Source Safe. Believe it or not, when I did my development work for Microsoft, they weren’t even using Source Safe internally (I’m serious, it was some hybrid of Source Safe an the next version with a lot of custom tweaks)!
To top it all off both Netbeans and Eclipse are totally free and open source. How much was Visual Studio Professional again? What about Architect? Ironically enough, Netbeans has all the features of Architect already and better reverse-engineering support with some of the Netbeans Project Add-Ons.
Verdict: Java IDEs smoke Visual Studio in all appreciable areas. Microsoft will tout it’s tight integration with it’s own tools, but even that is suspect because in practice, it’s quite limiting. Good Java IDEs are free, more capable, and significantly more flexible.
Community – C# vs. Java
The C# community is quite nice with sites like Code Project, Got Dot Net, MSDN and others. The Java community has similar sites as well so I consider then generally matched but with Java having the edge due to its maturity.
Where Java really shines is in the open source world. The open source community has embraced Java with both arms and it shows. The Apache Project alone contains dozens of major Java projects ranging from an entire application server (Geronimo, think IIS + BizTalk + a few other MS server products) to a fully-functional embedded database (Derby) to multiple sophisticated and powerful web application frameworks (Tapestry, Struts, etc), to logging frameworks (SLF4J, Log4J, Commons Logging), to a full mail server (James) to search engines, content managers, etc. And that is just Apache alone! Source Forge, Code Haus, and hundreds of other sites all provide major applications, utilities and tools. Some notable ones include Hibernate (object to database mapping, vastly superior to ADO.NET), Junit (automated unit testing framework), and Spring (inversion of control framework to build clean and flexible services).
It’s interesting to note that some of these libraries are being ported over to C# as well but that doesn’t change the fact that the open source innovation is on Java first, .NET second.
Verdict: Java by a mile. While good open source projects exist for .NET, many of the best are simply attempts to port them from Java. Additionally, open source projects exist for Java that can compete with any Microsoft capability and often do it better. IE. Tapestry and JSF are both better in my opinion then ASP.NET, Hibernate is superior to ADO.NET, etc.
-=-=-=-
Final Verdict: I’ve used Java and C# regularly for both my own personal applications as well as commercial applications. In general I say use the right tool for the job; C# for Windows GUI or game, Java for web or database apps, etc. If I had to pick only one tool for all jobs though, it would be Java for these reasons:
1)Flexibility – Java is simply more flexible in all major areas. .NET is tied to Microsoft’s platform and so you are stuck with what it can, and can’t, do. Java has no such limitations. Additionally, Java’s design is built around being flexible while .NET has no qualms with locking you to the “Microsoft way”. While this can make some projects work very well, it can also make “out of the box thinking” projects quite cumbersome. Additionally, Java just “supports more”. Chose your database, almost certainly a Java driver for it. Can’t say that for .NET. The same goes for pretty much any other aspect of Java.
2)Capability – Java is more powerful with both built-in APIs and open source APIs. It’s as simple as that. .NET might catch up in a few years, but that’s not currently the case and I don’t see it happening any time soon considering that Java’s adoption is 2 to 1 over .NET (see my links below).
3)Cost – Java can be developed and deployed for free with no loss of functionality or capabilities. Using Netbeans/Eclipse on Linux/Solaris/FreeBSD I can create a world-class enterprise application and deploy it to Linux or (other free OS) servers running Geronimo (or Tomcat or Jetty, etc) and MySQL (or Postgres or Derby, or HQL, etc) at no expense other then the physical hardware itself. Microsoft charges you every step of the way for every component.
-=-=-=-
While that is my full answer, I just have to refute some of the previous answers because they are simply wrong. PLEASE research this yourself before making a decision that you might regret. The experience you get in school will make your first job out of school much easier. Start on the wrong foot and you have to back track a bit.
From ChampionForever:
“Java from Sun MicroSystems is already a sunset technology. Bill Gates has pour all his resources to make sure .NET succeed.”
And how did you come up with this? I agree with Microsoft has poured it’s heart and soul into .NET and this has been a good thing but in no way shape or form is java a “sunset technology”.
In fact, I directly refute your answer with a link from a reputable and independent industry source:
http://www.forrester.com/Research/Document/Excerpt/0,7211,37356,00.html
From Superfly:
“Java is pretty much a dead language, i dont think new features (to the language itself,”
Please see the link above to see how a “dead language” has a double the adoption rate of your “superior” language.
Java 1.5 implemented the vast majority of features that C# had over Java. Generics, autoboxing, etc all made it in. The remaining features missing from Java, for the most part, we’re intentionally left out to avoid developer problems. Things like operator overloading and unsafe code blocks can be badly abused (inadvertently as well as intentionally). I do wish Java would pick up type-safe delegates though.
I’d also state that a language shouldn’t need huge underlying changes every release because that implies the language itself is deficient. For the most part Java has struck an excellent balance between developer capability and safety. This is part of the reason it’s so popular with enterprises. Sun simply advances the APIs themselves to ensure continuing support for new technologies.
“Java is a very old language, which will hopefully die soon, as C# is far superior”
Old in this case means tried and true (i.e. effective). Additionally, Sun has shown to be willing to evolve the language regularly to keep it viable and competitive. Just look at the last three releases to see this in action. As for the “far superior” comment, once again you say it without any supporting comments. I actually feel C# is a tiny bit better as a language then Java but all the downsides of .NET vs. the Java platform far outweigh the trivial language differences.
“also, if the poster below hasnt noticed, C# will run on linux and macintosh (through the use of Mono on linux, who cares about mac?)”
Yes, except Mono support isn’t complete, has significant bugs, porting issues, etc. While Java support on multiple platforms is available and works right out of the box, C# simply doesn’t work on multiple platforms without a great deal of work. I hope that Mono becomes a solid option, but it’s not there yet.
Educate yourself on the status of Mono here:
http://www.mono-project.com/Resources#API_completion_status_pages
“therefore MUCH faster
applications which would have NO chance of getting any reasonable performance on Java can be written in C#”
Ugh, this is completely false. Performance is both difficult to measure and subjective. It depends entirely on the application and what it’s doing. Java is faster in some areas; .NET is faster in some areas. Java’s ability to run on better hardware though makes it a clear winner overall. I can take a slow Java app and throw hardware at it to keep it going. A slow .NET app is going to die quite quickly as I just can’t scale it vertically (big machines as opposed to many machines). I will say that in the area of socket servers and socket-based applications (an area in which I am familiar) Java is a superior winner with full ePoll support as well as a much better selector model. Epoll is unavailable on Windows and Microsoft’s equivalent just doesn’t cut it.
“if you dont know what youre doing and write bad code, you shouldnt be programming anyways :)”
It must be nice to be able to pick your development team to completely avoid weaker developers. In the rest of the world, we’re stuck with who’s available and whom we can get. This means that the language defending me from poor developers is a Good Thing and can’t be under estimated.
I’d love the meet you in person, as apparently you are the only developer that was born fully capable of writing perfect code. The rest of us had to suffer through been poor coders before we learned better.
From Biondo_86:
“with c# you can learn asp.net and thats not possible in java.”
Yes, that’s because ASP.NET is a .NET-only technology. While I feel ASP.NET is better then the provided Servlet and JSP support in Java there are other technologies for Java that are much more capable then ASP.NET and still free. Personally, I feel Tapestry, Wicket, JSF, and others provide a much better web application environment overall.
“nowadays java is nearly becoming used only for small applets”
This is categorically false. In six years of commercial Java development, I’ve never had to write a single applet. I don’t even know any Java developers who’ve had to write an applet commercially. In fact, I’m really curious how you came to this conclusion at all. If you search on hotjobs.com and the other job sites for the word applet, you will see how uncommon it really is as compared to Java directly.
“but for whole websites or applications c# or vb.net are used more”
Please refer to my Forrester link above to lean how C# is NOT used more then Java. Again, where do you come up with these figures?