{"id":416,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=416"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Type Erasure","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=416","title":{"rendered":"Type Erasure"},"content":{"rendered":"<p><DIV id=PageTitle>Type Erasure<\/DIV><br \/>\n<BLOCKQUOTE><br \/>\n<P>When a generic type is instantiated, the compiler translates those types by a technique called <I>type erasure<\/I> \u2014 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><br \/>\n<P>For instance, <CODE>Box&lt;String&gt;<\/CODE> is translated to type <CODE>Box<\/CODE>, which is called the <I>raw type<\/I> \u2014 a raw type is a generic class or interface name without any type arguments. This means that you can&#8217;t find out what type of <CODE>Object<\/CODE> a generic class is using at runtime. The following operations are not possible: <\/P><br \/>\n<BLOCKQUOTE><PRE>public class MyClass&lt;E&gt; {<br \/>\n    public static void myMethod(Object item) {<br \/>\n        if (<B>item instanceof E<\/B>) {  \/\/Compiler error<br \/>\n            &#8230;<br \/>\n        }<br \/>\n        <B>E item2 = new E();<\/B>   \/\/Compiler error<br \/>\n        <B>E[] iArray = new E[10];<\/B> \/\/Compiler error<br \/>\n        <B>E obj = (E)new Object();<\/B> \/\/Unchecked cast warning<br \/>\n    }<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE>The operations shown in bold are meaningless at runtime because the compiler removes all information about the actual type argument (represented by the type parameter <CODE>E<\/CODE>) at compile time.<br \/>\n<P>Type erasure exists so that new code may continue to interface with legacy code. Using a raw type for any other reason is considered bad programming practice and should be avoided whenever possible. <\/P><br \/>\n<P>When mixing legacy code with generic code, you may encounter warning messages similar to the following: <\/P><br \/>\n<BLOCKQUOTE><PRE>Note: WarningDemo.java uses unchecked or unsafe operations.<br \/>\nNote: Recompile with -Xlint:unchecked for details.<br \/>\n<\/PRE><\/BLOCKQUOTE>This can happen when using an older API that operates on raw types, as shown in the following <A class=SourceLink href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/java\/generics\/examples\/WarningDemo.java\" target=_blank><CODE>WarningDemo<\/CODE><\/A> program:<br \/>\n<BLOCKQUOTE><PRE>public class WarningDemo {<br \/>\n    public static void main(String[] args){<br \/>\n        Box&lt;Integer&gt; bi;<br \/>\n        bi = createBox();<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Pretend that this method is part of an old library,<br \/>\n     * written before generics. It returns<br \/>\n     * Box instead of Box&lt;T&gt;.<br \/>\n     *\/<br \/>\n    static Box createBox(){<br \/>\n        return new Box();<br \/>\n    }<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE>Recompiling with <CODE>-Xlint:unchecked<\/CODE> reveals the following additional information:<br \/>\n<BLOCKQUOTE><PRE>WarningDemo.java:4: warning: [unchecked] unchecked conversion<br \/>\nfound   : Box<br \/>\nrequired: Box&lt;java.lang.Integer&gt;<br \/>\n        bi = createBox();<br \/>\n                      ^<br \/>\n1 warning<br \/>\n<\/PRE><\/BLOCKQUOTE><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Type Erasure When a generic type is instantiated, the compiler translates those types by a technique called type erasure \u2014 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 &hellip; <a href=\"https:\/\/www.strongd.net\/?p=416\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Type Erasure<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-416","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=416"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/416\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}