tags:

views:

5165

answers:

8

I just want to clarify one thing. This is not a question on which one is better, that part I leave to someone else to discuss. I don't care about it. I've been asked this question on my job interview and I thought it might be useful to learn a bit more.

These are the ones I could come up with:

  • Java is "platform independent". Well nowadays you could say there is the Mono project so C# could be considered too but I believe it is a bit exaggerating. Why? Well, when a new release of Java is done it is simultaneously available on all platforms it supports, on the other hand how many features of C# 3.0 are still missing in the Mono implementation? Or is it really CLR vs. JRE that we should compare here?
  • Java doesn't support events and delegates. As far as I know.
  • In Java all methods are virtual
  • Development tools: I believe there isn't such a tool yet as Visual Studio. Especially if you've worked with team editions you'll know what I mean.

Please add others you think are relevant.

Update: Just popped up my mind, Java doesn't have something like custom attributes on classes, methods etc. Or does it?

+2  A: 

Generics:

With Java generics, you don't actually get any of the execution efficiency that you get with .NET because when you compile a generic class in Java, the compiler takes away the type parameter and substitutes Object everywhere. For instance if you have a Foo<T> class the java compiler generates Byte Code as if it was Foo<Object>. This means casting and also boxing/unboxing will have to be done in the "background".

I've been playing with Java/C# for a while now and, in my opinion, the major difference at the language level are, as you pointed, delegates.

bruno conde
This is wrong, generics erasure or reification (Java and C# respectively) doesn't necessarily affect performance.
Miguel Ping
You're confusing autoboxing with casting.
JesperE
No, bruno is right about the performance difference. There's no way of getting the equivalent of a List<byte> (generically) in Java. You'd have to have a List<Byte> which would incur boxing penalties (time and memory).
Jon Skeet
The (un)boxing only happens for boxed types, which are primitive types.
Miguel Ping
Please see this article: http://www.jprl.com/Blog/archive/development/2007/Aug-31.html
bruno conde
Miguel: Sure, but at that point it certainly affects performance! (And there's also no need for the CLR to perform a casting type check when fetching from a generic list; it knows the type will be correct.)
Jon Skeet
+6  A: 

See Comparison of C Sharp and Java.

gimel
+89  A: 

Not all of your summary is correct:

  • In Java methods are virtual by default but you can make them final. (In C# they're sealed by default, but you can make them virtual.)
  • There are plenty of IDEs for Java, both free (e.g. Eclipse, Netbeans) and commercial (e.g. IntelliJ IDEA)

Beyond that (and what's in your summary already):

  • Generics are completely different between the two; Java generics are just a compile-time "trick" (but a useful one at that). In C# and .NET generics are maintained at execution time too, and work for value types as well as reference types, keeping the appropriate efficiency (e.g. a List<byte> as a byte[] backing it, rather than an array of boxed bytes.)
  • C# doesn't have checked exceptions
  • Java doesn't allow the creation of user-defined value types
  • Java doesn't have operator and conversion overloading
  • Java doesn't have iterator blocks for simple implemetation of iterators
  • Java doesn't have anything like LINQ
  • Partly due to not having delegates, Java doesn't have anything quite like anonymous methods and lambda expressions. Anonymous inner classes usually fill these roles, but clunkily.
  • Java doesn't have expression trees
  • C# doesn't have anonymous inner classes
  • C# doesn't have Java's inner classes at all, in fact - all nested classes in C# are like Java's static nested classes
  • Java doesn't have static classes (which don't have any instance constructors, and can't be used for variables, parameters etc)
  • Java doesn't have any equivalent to the C# 3.0 anonymous types
  • Java doesn't have implicitly typed local variables
  • Java doesn't have extension methods
  • Java doesn't have object and collection initializer expressions
  • The access modifiers are somewhat different - in Java there's (currently) no direct equivalent of an assembly, so no idea of "internal" visibility; in C# there's no equivalent to the "default" visibility in Java which takes account of namespace (and inheritance)
  • The order of initialization in Java and C# is subtly different (C# executes variable initializers before the chained call to the base type's constructor)
  • Java doesn't have an equivalent of the using statement for simplified try/finally handling of resources
  • Java doesn't have properties as part of the language; they're a convention of get/set/is methods
  • Java doesn't have the equivalent of "unsafe" code
  • Interop is easier in C# (and .NET in general) than Java's JNI
  • Java and C# have somewhat different ideas of enums. Java's are much more object-oriented.
  • Java has no preprocessor directives (#define, #if etc in C#).
  • Java has no equivalent of C#'s ref and out for passing parameters by reference
  • Java has no equivalent of partial types
  • C# interfaces cannot declare fields
  • Java has no unsigned integer types
  • Java has no language support for a decimal type. (java.math.BigDecimal provides something like System.Decimal - with differences - but there's no language support)
  • Java has no equivalent of nullable value types
  • Boxing in Java uses predefined (but "normal") reference types with particular operations on them. Boxing in C# and .NET is a more transparent affair, with a reference type being created for boxing by the CLR for any value type.

This is not exhaustive, but it covers everything I can think of off-hand.

Jon Skeet
I'm not sure on how static classes work in C#, but Java has enum classes which ought to be similar.
JesperE
@JesperE: No, enum classes aren't really like static classes in C#. You can still pass enums as parameters, use them as variables, define instance mtehods etc in Java. In C# static classes are *just* used for utility methods, basically. (There can be static state in the class, but no instance state)
Jon Skeet
"Java doesn't have static classes (which don't have any constructors..." Apart from static constructors! :)
Daniel Earwicker
@Earwicker: Edited appropriately. Pedantry is good :)
Jon Skeet
When you see it spelled out like that, I suddenly get a better understanding of why I have had this feeling that Java is inferior to C#.
Morten Christiansen
"Java doesn't have static classes (which don't have any instance constructors, and can't be used for variables, parameters etc)" -- I've never really been sure what the point of static classes are... I can have a class containing only static methods in Java.
R. Bemrose
@R Bemrose: In Java, if you want to make a class uninstantiable, you have to explicitly declare a private constructor. That doesn't stop you from creating an instance within the class. It also doesn't stop people from trying to use the type for variables etc. Static classes signal *intent*.
Jon Skeet
"Java doesn't have anonymous types". Not true. In order to have anonymous inner classes, anonymous types are a must. With that said, to make anonymous types really useful, you need type inference. C# has this, Java does not.
Daniel Spiewak
True, there are anonymous types. I will clarify it to "Java has no equivalent of C# anonymous types."
Jon Skeet
"Java doesn't have the equivalent of "unsafe" code"... Java does support the native keyword which is less functional, but does still allow the programmer to step outside of the managed runtime.
Michael Meadows
@Michael: It's not the same thing at all IMO. That would be akin to P/Invoke, not unsafe C# code.
Jon Skeet
@Morten Christiansen: Yeah, it seems like that. But simplicity (in terms of simple definition - simple usage is another story) is also a strength, so Java might be superior ;)
Brian Schimmel
@Brian: I think Java generics and the details of inner classes pretty quickly quash the idea of Java achieving superiority through simplicity ;)
Jon Skeet
C# vs java = Giant list of what java doesn't have and what C# does...
CrazyJugglerDrummer
Wow. A lot of good info here. Like any large subject, of course one must omit more than one covers. But these individual points are all worth reading. Great answer.
Warren P
This list makes Java pretty outdated language.
jpartogi
@jpartogi: Well, Java 7 should help on at least some of these points. And there's more to life than being the new and shiny thing on the block. Having said that, I still definitely prefer C# to Java :)
Jon Skeet
Thanks Jon. I will consider learning C# now. It looks very good. Only God knows when Java 7 will be out. =D
jpartogi
+1: This is a nice reference to have.
RedFilter
+9  A: 

The following is a great in depth reference by Dare Obasanjo on the differences between C# and Java. I always find myself referring to this article when switching between the two.

http://www.25hoursaday.com/CsharpVsJava.html

Winston Smith
Shame it hasn't been updated for C# 3 yet :(
Jon Skeet
claws
@claws: I haven't got time to do everything I'd like to.
Jon Skeet
@Jon Chuck Norris always has time...
Winston Smith
LOL! Jon, has no time! :P
claws
@Winston: We need a "differences between Chuck Norris and Jon Skeet" list: *1) Chuck Norris always has time; Jon must modify the `TimeDate` class in order to always have time, and has not had time to yet :(*
RedFilter
+1  A: 

C# has automatic properties which are incredibly convenient and they also help to keep your code cleaner, at least when you don't have custom logic in your getters and setters.

Morten Christiansen
+3  A: 

Another good resource is http://www.javacamp.org/javavscsharp/ This site enumerates many examples that ilustrate almost all the differences between these two programming languages.

About the Attributes, Java has Annotations, that work almost the same way.

Rafael Romão
+2  A: 

I found this one http://msdn.microsoft.com/en-us/library/ms836794.aspx It covers both the similarity and difference between C# and java.

Bipul
A: 

C# is a language of the 21st century, we have LINQ, we have amazing stuff like the WPF.

Java is stuck in the early 90s - and it's very hard to develop software in Java, because all the things that are easy to do in C# are a pain in Java.

I stopped bothering with this suboptimal toy language when I tried to use this JSF once. Never again.

Turing Complete
Java has a pretty good wpf analog, and the next version is laying the foundation for their own linq-ish system.
Joel Coehoorn
@Joel So is it here yet?
Jouke van der Maas