Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The difference comes down to a design decision by Microsoft and Sun.</p> <p><a href="http://en.wikipedia.org/wiki/Generics_in_Java" rel="noreferrer">Generics in Java</a> is implemented through <a href="http://java.sun.com/docs/books/tutorial/java/generics/erasure.html" rel="noreferrer">type erasure</a> 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:</p> <p>From The Java Tutorials, <a href="http://java.sun.com/docs/books/tutorial/java/generics/erasure.html" rel="noreferrer">Generics: Type Erasure</a>:</p> <blockquote> <p>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.</p> </blockquote> <p>However, with <a href="http://en.wikipedia.org/wiki/Generic_programming#Generic_programming_in_C.23_and_.NET" rel="noreferrer">generics in C# (.NET)</a>, 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.</p> <p>From Wikipedia:</p> <blockquote> <p>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.</p> </blockquote> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload