Throughout my university career I used Java to code projects until I started working which is where I had to delve into the C# realm. Though the .Net library is fairly extensive I can't help but feel that there is something missing in C# as compared to Java. I would like to know if Java is any better than C# or vice versa from the experts out there. What would you use to develop both complex and fairly trivial applications? What are some of the advantages and disadvantages of using these 2 technologies over the other?
More sugar, less guts.
C# has a whole shopping list of syntactic sugar - properties, linq, closures, delegates - which makes typing things easier.
Java has a wider range of libraries, both in the standard libraries and I get the impression in OSS. The standard libraries are often very good. The Sun JVM is also recognised as being very good.
I've spent the last couple of months on a project working with Windows.Forms in C#, and very much the comment stands - syntax sugar - closures rather than anonymous classes, events and delegates rather than maintainings list of handlers - are the main differences. Attempts to add queries to Java which were not language integrated have been made, but failed - with static imports and anonymous classes you'd have to write
count(foo, new Predicate<Integer>(){ public bool Test(Integer i) { return i < 10; } })
instead of Linq's
foo.Count(i=>i<10)
but they are completely equivalent. To implement Linq to SQL, you'd have to do some analysis of the byte code as Java does not have a built-in expression tree, but JVM byte code is at a high enough level that it is not significantly more complicated to process than an AST.
The C# OSS (NUnit,Saxon.Net) I've used follow from Java rather than innovating itself - most of the innovative C# stuff seems to come straight from Microsoft research, but the Visual studio IDE is better than the IDEs I've used for Java, especially for GUI work.
Dare Obasanjo wrote a very good article on Java vs C# way back in 2001, but it still has a lot of valid points, definitely worth a read. Right here on Kuro5hin.
EDIT: I bow to these other gentlemen, their links are preferable to mine and goes to the same article in a more readable format.
First of all, none is better than the other. Each one has pros and cons. For instance Java is multi-platform and C# it's not (although they try to make it with mono, but it's not the same). On the other hand, Java tends to be a little more slower than C#, at least from my personal perspective.
This all comes down to personal taste, or business decisions.
For me personally the main reason i pick up C# in favour of java is that java doesn't have Delegates (function references) so there's no (sane) way to implement a callback pattern. I find all the anonymous classes and interfaces to get Event based programing going in Java very very tiresome. Also Swing/SWT/AWT are all nice but i find WinForms or even GTK# generally more enjoyable and much much more responsive.
Cant really say which one is better as they are fundementally different (eventhough they share alot of common syntax).
I'll scrub my last answer and write one line of why I prefer C# to Java:
- No checked exceptions
Update:
Having recently done some work with Awt and Swing for a degree I'm doing, I'll add to this
- The Publish/Subscribe pattern that Java uses for Events in AWT
I like the pattern, the ActionListeners and the various adapters, but I think once you've used delegates in C#, the latter is far faster to work with (even with auto-generated code that IDEs like Netbeans produce)
I think the programmer is more important than the technology used. You can do fantastic software with either Java or C#. Don't get hung up which language is better. Instead focus on mastering what you use daily.
In my experience of the languages, there's very little in Java but not in C# that I actually want. Enums and static imports spring to mind, but that's about it. There are plenty of things I don't like in Java:
- Type erasure in generics
- Inner classes (as opposed to just nested classes)
- Calling static methods "through" references e.g.
myThread.sleep(1000)
- Anonymous inner classes (where a delegate almost always does the job)
- Checked exceptions
There are many things in C# which I wish Java had:
- Language-defined properties (instead of just conventions)
- Delegates and related features:
- Events
- Lambda expressions and anonymous methods (big one!)
- Expression trees
- Extension methods
- Anonymous types
- Methods are non-virtual by default
- Saner handling of readonly/final fields
- Explicit interface implementation
- Iterator blocks
- The
using
statement - User-defined value types (such as
DateTime
,decimal
,Guid
) and nullable value types to go with them
Java 1.4 and C# 1.0 were very similar. Now the two languages (when written idiomatically) are quite different in many cases. I miss LINQ almost every day when I'm coding in Java.
The differences in generics mostly come down in C#'s favour IMO, although the approach to variance in Java definitely has its advantages. C# 4.0 will get limited variance, which will at least help.
I'm a Java developer, and have only read about .Net.
My opinion: C# is a more modern version of Java (properties, delegates, other stuff). Some of the more strict Java 'features' gave been dropped (checked exceptions).
C# is being evolved much more rapidly than Java, but you might argue that is a bad thing.
Java has a very rich ecosystem. Every major IT player has something in the Java arena, with the exception of Microsoft.
In the Javasphere you have a lot of choice: IDEs, frameworks, runtime environments, application servers. The C# arena offers much less choice.
.Net is a better choice for Windows desktop applications, Java is better suited for large scale server applications.
Please note these are very broad generalisations.
For years C# was playing catchup with Java. That ended with C#/.Net 3.0/3.5 and now it's (mostly) the other way around. What does C# have that Java doesn't?
- Closures;
- Runtime generics;
- Generics of primitive types (benchmarks of this sorting a list of a million ints vs a million Integer objects have revealed a factor of 3 improvement);
- Delegates;
- Events;
- LINQ;
- Extension methods;
- First-class properties;
- Operator overloading;
- Indexers;
- Anonymous types;
- Expression trees;
Using
blocks;- No checked exceptions. Hooray!
- Decimal type;
- As of C#: the
dynamic
type, which is basically duck typing.
And Java has:
- WAY, way better enums. C# enums are just ints with a stripe painted down the side. Java treating enums as objects with behaviour is far superior (imho);
- I know this will be controversial: better IDEs, particularly in the realm of code refactoring where Visual Studio (without Resharper) is still lagging far behind Intellij, Eclipse and possibly even Netbeans;
- It runs on Linux. Mono notwithstanding, Windows is by far .Net's biggest achilles heel (imho);
- It's free. Java 6 + Glassfish + Eclipse + Linux costs you... nothing. Now do the same with .Net + IIS + VS + R# + Windows Server...;
- IMHO the Java 5 concurrency utils package is still superior to any sort of concurrency tools that I've seen in C# to date; and
- Significantly more open source projects but that lead is being rapidly diminished.
And sadly that's about all I can think of for ticks in the Java column.
For me, as a Windows programmer, C# wins but this isn't because of all the advanced stuff like generics, LINQ, etc. What I really, really like is that there is a single IDE (which is free in the express edition). Using this IDE you can develop console apps, windows apps (with an excellent form designer built in), web apps, database apps etc which will all build and run without having to carefully set up lots of different components.
NB For comparison, I installed RAD and couldn't even find where to start typing the code in.
Aside from the language-specific details, there is one difference between Java and C# that makes all the difference when choosing which programming language to use for a project. The difference is that C# is a Microsoft language, and Java is more or less open-source. This has three major implications on a new project:
C# will only run on Microsoft-approved operating systems. This means kissing Unix and Linux goodbye at square one. Then again, if you're aiming at deploying on MS servers, C# is perfect.This item is wrong. Thanks to ShuggyCoUk for the correcting comment.C# comes with whatever Microsoft decided to give you, plus the MSDN forums. The number of Java extensions and open-source platforms, on the other hand, is too numerous to count. In short, there's a helluva lot more free Java code out there to use than C# code. If you have need for such extensions for C#, you may have to dig deep or (God forbid) develop them yourself.
C# libraries are usually well tested and reliable, and come with some guarantee of quality. Java, on the other hand, has a few tenacious bugs within its core libraries, and I haven't seen a single open-source platform without a known bug list. Take this into consideration when thinking about using third-party code.
Hope this helps...
Yuval =8-)