views:

4777

answers:

14

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?

+7  A: 

This is a nice read: C# From a Java Developer's Perspective

Zaagmans
+2  A: 

Jump to the conclusion of this comparison of C# and Java.

michelemarcon
A: 

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.

Pete Kirkham
I'm not a C# developer, but from what I read LINQ is much more than just syntactic sugar. Infact I'd love to have something similar in the Java world.
Joachim Sauer
There are functions to implement the features the syntax provides, but the big win is the sugar - writing "x where x.foo > 7" instead of "SelectFrom(x, new Predicate<X>(){ public boolean test (X x) { return x.foo > 7;}})". The sugar makes the feature usable.
Pete Kirkham
The Demeter J project<http://www.ccs.neu.edu/research/demeter/DemeterJava/use/ ; does pretty well what Linq does in terms of replacing navigation with queries, but being a non-Sun syntax extension to Java it never caught on.
Pete Kirkham
Linq is a mathematical concrete construct
Aggelos Mpimpoudis
Please provide a recognised example from concrete mathematics and corresponding linq expression, either in sugared `from\where\select` form or the dotted form.
Pete Kirkham
Wow Pete, It seems like a bit of a crime to see you downvoted so much. You won't remember me, but I remember you from the Java forums. Regardless, done my part to try and restore order :)
Noon Silk
Wow, dunno why all the haters are hating on this.
Kaleb Brasee
A: 

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.

Stefan Thyberg
+3  A: 

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.

rogeriopvl
+1  A: 

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).

Martijn Laarman
+11  A: 

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)

Chris S
+1 amen. Stupid people downvoting this.
cletus
+2  A: 

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.

Dominic
+29  A: 

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.

Jon Skeet
Could you elaborate on the difference between Anonymous inner classes (a con of Java) and Anonymous types (a pro for C#)? What's the difference?
Joachim Sauer
@saua: They're entirely different things. Anonymous types in C# are basically immutable reference types used usually used for projections within a method. Anonymous inner types in Java are usually used for overriding methods of a based class.
Jon Skeet
You actually want static imports? You can have 'em, as far as I'm concerned.
Michael Myers
"- Inner classes (as opposed to just nested classes)" Doesn't Java have both?
Michael Myers
@mmyers: Yes, Java has both - it's only inner classes I don't like. Nested classes are fine, although I find they're simpler to reason about in c# than in Java. As for static imports - yes, they're really handy in places. C# extension method discovery was badly designed IMO.
Jon Skeet
Disagree about const/readonly/final fields (I find the C# version confusing) but +1 for the other items
finnw
+3  A: 

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.

Jeroen van Bergen
+73  A: 

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.

cletus
Not sure I agree with you about playing catchup having used both since 1.0/1.4 (definitely not the IDE part :). Also Visual Studio is free, IIS is free, codeplex has a lot of C# OS projects as does googlecode. However a lot of them a java ports
Chris S
MS has free cut down versions of some of their products. Not quite the same as being free. Also, that Java is now playing catchup is empirically undeniable, hence all the Java 7 JSRs about closures, extension methods, properties, reified generics, etc.
cletus
I don't agree with your analogy of "dynamic" to "var". "var" is entirely statically typed - unless you're using it for anonymous types, it's just syntactic sugar for a normal variable declaration.
Jon Skeet
Perhaps I was phrasing that wrong: anonymous types was really what I was getting at.
cletus
Jon, it's not really just syntactical sugar either, because of anonymous types.
Tom Hawtin - tackline
"'var' is a wrapper for reflection" - Say what? I don't know what you mean there, and I'm not sure that you do either. var is static type inference at compile time, and is gone by the time bytecode is generated.
Anthony
I meant I don't think C# has ever been catching up, the opposite. Perhaps with generics (based on the release date)
Chris S
Java was certainly far more fully featured than .Net 1.x. .Net 2.0 was a more mixed bag. .Net 3.0--feature-wise at least--pulled in front by a pretty wide margin.
cletus
In terms of concurrency stuff: wait til Parallel Extensions hits with .NET 4.0.
Jon Skeet
Do you mean language features of C# or the BCL? 1.0 was released the same time 1.4 but I can't think what Java had that wasn't in in BCL. I know image manipulation wasn't fun in Java back then :)
Chris S
@Jon: yeah yeah but C# 4.0 isn't now, hence Java still (for a time at least) has one advantage there. :)
cletus
.net 2.0 came out late 2005. java 5 came out a year earlier. seehttp://en.wikipedia.org/wiki/.NET_Framework#.NET_Framework_1.0 and http://en.wikipedia.org/wiki/Java_version_history
cletus
@cletus: So why have you included "dynamic" in the C# side? :)
Jon Skeet
.NET 1.0 release Feb 2002, Java 1.4 release Feb 2002
Chris S
I remain surprised at how weak concurrency support and collections are, when compared to Java, in C# 3.0. (I haven't looked into 3.5 to see what has changed there.) Other than this, with 3.0, C# has pulled ahead of Java in the "useful feature count" comparison.
Eddie
cletus, you call Java Enums superior. I don't agree. The power of C# enums lies in the fact it is int (or long, or byte, or whatever basetype you chose), which makes it light, and enables stuff likes flagging and easy C/C++ interop. The enum in C# is just an syntactical shell plus some resolvement methods. That doesn't make it inferior, but different.
Dykam
You can do that with ints. You don't an enum type for that. The beauty of the Java enum type is it gives you something more than an int.
cletus
checked exceptions ought to go on the Java side. at least having them as a compiler option in c# would have been nice. They document part of a contract very nicely, a part taht might otherwise be hard to find, unless of course you hit the wall an get "unhandled exception"
Rune FS
A: 

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.

Hmm, I've been doing Java for several years and this is the first time I've heard of RAD.
Michael Myers
IBM Rational Application Developer
LOL, RAD is basically just like Eclipse. If you can't find where to type code in, it's because you don't know Eclipse.
Kaleb Brasee
+2  A: 

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:

  1. 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.

  2. 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.

  3. 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-)

Yuval
Can you think of (OS) projects/software in #2 that Java has and C# doesn't?
Chris S
@Chris I'm a Java programmer, so I can't say I know the C# world well enough to answer this. I'm sure there are lots of parallels, but I'm convinced that whatever infrastructure you may need, the Java community either has it or is developing it. =8-)
Yuval
If you don't have a known bug or two it's because you don't publish it or almost nobody is using your software, not because it's without bugs.
wowest
@wowest What I meant to say is that most Java open-source platforms are in perpetual development stages, and precious few can be truly described as 'stable'. =8-)
Yuval
-1 "C# will only run on Microsoft-approved operating systems." this is both factually wrong (it's an ECMA spec) and demonstrably true (mono)
ShuggyCoUk
@ShuggyCoUk I stand corrected.
Yuval
-1 retracted, thanks for updating (and no need to credit me feel free to simply eliminate the section if you prefer)
ShuggyCoUk
@ShuggyCoUk I'd rather not remove the item... this is an important point, that I thought was true for years (which it was up to a certain point...), and I believe others are unaware of this as I was.
Yuval
A: 

I'd say LINQ's expression trees if only I know what they were. :)