209
votes

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?

4
  • 1
    Languages are different from language implementations, which are also different from libraries. What are you trying to compare? Nov 17, 2008 at 10:14
  • 1
    See Comparison of C Sharp and Java.
    – gimel
    Nov 17, 2008 at 10:15
  • 2
    I found this one msdn.microsoft.com/en-us/library/ms836794.aspx It covers both the similarity and difference between C# and java.
    – Bipul
    Jun 10, 2010 at 3:43
  • 1
    You can get many of the things mentioned below about Java with the right libraries. Check for example this valid Java code: new String[] { "james", "john", "john", "eddie" }.where(startsWith("j")).distinct(); It uses a library called lombok-pg. Can be found at github.com/nicholas22/jpropel
    – NT_
    Oct 7, 2011 at 20:35

7 Answers 7

328
votes

Comparing Java 7 and C# 3

(Some features of Java 7 aren't mentioned here, but the using statement advantage of all versions of C# over Java 1-6 has been removed.)

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

58
  • 24
    @Brian: I think Java generics and the details of inner classes pretty quickly quash the idea of Java achieving superiority through simplicity ;)
    – Jon Skeet
    Aug 18, 2009 at 16:39
  • 8
    @OrangeDog: People arguing that are mostly kidding themselves, IMO. It's hard to see how being forced to write an explicit try/finally block is less error-prone than a using statement, IMO. Most of C#'s "extra" features compared with Java mean that you can get away with writing less code, and that that code can be more readable.
    – Jon Skeet
    Jan 19, 2011 at 6:26
  • 17
    @OrangeDog: How impartial are you, out of interest? Yes, I'm a C# enthusiast, but with pretty significant Java experience too - it's my day job, after all. It's not like I'm ignorant of how to use Java effectively.
    – Jon Skeet
    Jan 19, 2011 at 9:50
  • 8
    @OrangeDog: For a start, most developers don't write code using "unsafe" as far as I'm aware, so that's a red herring. I also think that provability is a red herring - I don't think formal provability has very much to do with how easy it is for a human to reason about code. My point is that as someone who is pretty experienced in both Java and C#, I find C# to be a far superior language in terms of productivity and readability. If you feel the opposite, could you clarify your levels of experience in both languages? I think it's pretty relevant to the discussion.
    – Jon Skeet
    Jan 19, 2011 at 12:06
  • 21
    @OrangeDog: Furthermore, your claim that "Being able to do more things increases the likelyhood that you'll do them wrong" is also a fallacy IMO... because it assumes that if you can't do something using a feature of the language which makes it easy, you won't have to do it at all. That's simply untrue - often the tasks you need to accomplish in Java and C# are the same, but due to missing features, Java makes it harder to accomplish those tasks correctly. By simplifying the task, the feature decreases the likelihood that you'll do it wrong.
    – Jon Skeet
    Jan 19, 2011 at 19:18
24
votes

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

3
  • 2
    @Jon Skeet: you are the most C# active developer. Why don't you maintain your version of C# & Java differences. I bet people would love to read it.
    – claws
    Aug 9, 2010 at 19:11
  • 1
    @claws: I haven't got time to do everything I'd like to.
    – Jon Skeet
    Aug 9, 2010 at 19:17
  • 19
    @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 :( Oct 20, 2010 at 15:51
11
votes

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.

10
votes

Features of C# Absent in Java • C# includes more primitive types and the functionality to catch arithmetic exceptions.

• Includes a large number of notational conveniences over Java, many of which, such as operator overloading and user-defined casts, are already familiar to the large community of C++ programmers.

• Event handling is a "first class citizen"—it is part of the language itself.

• Allows the definition of "structs", which are similar to classes but may be allocated on the stack (unlike instances of classes in C# and Java).

• C# implements properties as part of the language syntax.

• C# allows switch statements to operate on strings.

• C# allows anonymous methods providing closure functionality.

• C# allows iterator that employs co-routines via a functional-style yield keyword.

• C# has support for output parameters, aiding in the return of multiple values, a feature shared by C++ and SQL.

• C# has the ability to alias namespaces.

• C# has "Explicit Member Implementation" which allows a class to specifically implement methods of an interface, separate from its own class methods. This allows it also to implement two different interfaces which happen to have a method of the same name. The methods of an interface do not need to be public; they can be made to be accessible only via that interface.

• C# provides integration with COM.

• Following the example of C and C++, C# allows call by reference for primitive and reference types.

Features of Java Absent in C#

• Java's strictfp keyword guarantees that the result of floating point operations remain the same across platforms.

• Java supports checked exceptions for better enforcement of error trapping and handling.

9
votes

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.

5
votes

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.

7
  • This is wrong, generics erasure or reification (Java and C# respectively) doesn't necessarily affect performance. Nov 17, 2008 at 10:17
  • You're confusing autoboxing with casting.
    – JesperE
    Nov 17, 2008 at 10:21
  • 3
    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
    Nov 17, 2008 at 10:26
  • The (un)boxing only happens for boxed types, which are primitive types. Nov 17, 2008 at 10:30
  • 1
    Please see this article: jprl.com/Blog/archive/development/2007/Aug-31.html Nov 17, 2008 at 10:36
0
votes

Please go through the link given below msdn.microsoft.com/en-us/library/ms836794.aspx It covers both the similarity and difference between C# and java

Not the answer you're looking for? Browse other questions tagged or ask your own question.