Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To complete the already very complete Jon Skeet's answer, you have to realize the concept of <strong><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.6" rel="noreferrer">type erasure</a></strong> derives from a need of <strong>compatibility with previous versions of Java</strong>.</p> <p>Initially presented at EclipseCon 2007 (no longer available), the compatibility included those points:</p> <ul> <li>Source compatibility (Nice to have...)</li> <li>Binary compatibility (Must have!)</li> <li>Migration compatibility <ul> <li>Existing programs must continue to work</li> <li>Existing libraries must be able to use generic types</li> <li>Must have!</li> </ul></li> </ul> <p>Original answer:</p> <p>Hence:</p> <pre><code>new ArrayList&lt;String&gt;() =&gt; new ArrayList() </code></pre> <p>There are propositions for a greater <strong><a href="http://www.weiqigao.com/blog/2007/01/20/java_generics_let_the_other_shoe_drop.html" rel="noreferrer">reification</a></strong>. Reify being "Regard an abstract concept as real", where language constructs should be concepts, not just syntactic sugar.</p> <p>I should also mention the <a href="http://java.sun.com/javase/6/docs/api/java/util/Collections.html#checkedCollection%28java.util.Collection%2C%20java.lang.Class%29" rel="noreferrer"><code>checkCollection</code></a> method of Java&nbsp;6, which returns a dynamically typesafe view of the specified collection. Any attempt to insert an element of the wrong type will result in an immediate <code>ClassCastException</code>.</p> <p>The generics mechanism in the language <strong>provides compile-time (static) type checking, but it is possible to defeat this mechanism with unchecked casts</strong>.</p> <p>Usually this is not a problem, as the compiler issues warnings on all such unchecked operations.</p> <p>There are, however, times when static type checking alone is not sufficient, like:</p> <ul> <li>when a collection is passed to a third-party library and it is imperative that the library code not corrupt the collection by inserting an element of the wrong type.</li> <li>a program fails with a <code>ClassCastException</code>, indicating that an incorrectly typed element was put into a parameterized collection. Unfortunately, the exception can occur at any time after the erroneous element is inserted, so it typically provides little or no information as to the real source of the problem.</li> </ul> <hr> <p>Update July 2012, almost four years later:</p> <p>It is now (2012) detailed in "<a href="http://docs.oracle.com/javame/test-tools/sigtest/2_2/html/a-compat.html#Z4000c521043377" rel="noreferrer">API Migration Compatibility Rules (Signature Test)</a>"</p> <blockquote> <p>The Java programming language implements generics using erasure, which ensures that legacy and generic versions usually generate identical class files, except for some auxiliary information about types. Binary compatibility is not broken because it is possible to replace a legacy class file with a generic class file without changing or recompiling any client code.</p> <p>To facilitate interfacing with non-generic legacy code, it is also possible to use the erasure of a parameterized type as a type. Such a type is called a <strong>raw type</strong> (<a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.8" rel="noreferrer">Java Language Specification 3/4.8</a>). Allowing the raw type also ensures backward compatibility for source code.</p> <p>According to this, the following versions of the <code>java.util.Iterator</code> class are both binary and source code backward compatible:</p> </blockquote> <pre><code>Class java.util.Iterator as it is defined in Java SE version 1.4: public interface Iterator { boolean hasNext(); Object next(); void remove(); } Class java.util.Iterator as it is defined in Java SE version 5.0: public interface Iterator&lt;E&gt; { boolean hasNext(); E next(); void remove(); } </code></pre>
 

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