views:

8873

answers:

4
+23  Q: 

C# vs Java generics

I have heard that the Java implementation of Generics is not as good as the C# implementation. In that the syntax looks similar, what is it that is substandard about the Java implementation, or is it a religious point of view?

+11  A: 

Comprehensive comparison here, with some links.

Strelok
+8  A: 

The difference comes down to a design decision by Microsoft and Sun.

Generics in Java is implemented through type erasure by the compiler, which means that the type checking occurs at compile time, and the type information is removed. This approach was taken to keep the legacy code compatible with new code using generics:

From The Java Tutorials, Generics: Type Erasure:

When a generic type is instantiated, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

However, with generics in C# (.NET), there is no type erasure by the compiler, and the type checks are performed during runtime. This has its benefits that the type information is preserved in the compiled code.

From Wikipedia:

This design choice is leveraged to provide additional functionality, such as allowing reflection with preservation of generic types, as well as alleviating some of the limitations of erasure (such as being unable to create generic arrays). This also means that there is no performance hit from runtime casts and normally expensive boxing conversions.

Rather than saying ".NET generics is better than Java generics", one should look into the difference in the approach to implement generics. In Java, it appears that preserving compatibility was a high priority, while in .NET (when introduced at version 2.0), the realizing the full benefit of using generics was a higher priority.

coobird
It sounds like .NET generics /are/ better than Java generics. Java designers took a sub-optimal growth path in order to provide backwards compatibility. That's perfectly acceptable, but it doesn't give them immunity from analysis.
Tom
+38  A: 

streloksi's link does a great job of breaking down the differences. The quick and dirty summary though is ...

In terms of syntax and usage. The syntax is roughly the same between the languages. A few quirks here and there (most notably in constraints). But basically if you can read one, you can likely read/use the other.

The biggest difference though is in the implementation.

Java uses the notion of type erasure to implement generics. In short the underlying compiled classes are not actually generic. They compile down to Object and casts. In effect Java generics are a compile time artifact and can easily be subverted at runtime.

C# on the other hand, by virtue of the CLR, implement generics all they way down to the byte code. The CLR took several breaking changes in order to support generics in 2.0. The benefits are preformance improvements, deep type safety verification and reflection.

Again the provided link has a much more in depth breakdown I encourage you to read

JaredPar
+1  A: 

Also found this conversation with Anders Hejlsberg that may be interesting too. But +1 for the answer linking to Jonathan Pryor's blog post.

IgorK