Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!news1.google.com!newsfeed2.dallas1.level3.net!news.level3.com!bos-service1.raytheon.com!bos-service2b.ext.ray.com.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.java.programmer Subject: Re: Java generics and type erasure From: Ian Shef References: <9d4c2b16-beb5-40b1-87a2-f03e971efeed@k17g2000vbn.googlegroups.com> Message-ID: User-Agent: Xnews/06.08.25 Lines: 109 Date: Tue, 24 May 2011 20:04:58 GMT NNTP-Posting-Host: 147.24.143.106 X-Complaints-To: news@ext.ray.com X-Trace: bos-service2b.ext.ray.com 1306267498 147.24.143.106 (Tue, 24 May 2011 20:04:58 UTC) NNTP-Posting-Date: Tue, 24 May 2011 20:04:58 UTC Organization: Raytheon Company Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4547 Lew wrote in news:irfgkl$lca$1@news.albasani.net: > John B. Matthews wrote: >> Marcin Pietraszek wrote: > >>> Anybody could explain me why in second example (line commended with >>> "compilation failure") compilation fails? > >> You neglected to specify the type parameter for foo2, specified in the >> declaration Foo. Without the actual type,, the compiler >> can only infer that get() returns Object, as would have been the case >> prior to generics: >> >> import java.util.*; >> >> public class Foo { >> >> private Map bar = new HashMap> Integer>(); >> >> public static void main(String... args) { >> Foo foo1 = new Foo(); >> Integer x1 = foo1.bar.get("x"); // ok >> >> Foo foo2 = new Foo(); >> Integer x2 = foo2.bar.get("x"); // compilation failure >> } >> >>> Do you know any detailed description on how and when type erasure >>> works in java? > >> "All of these parameterized types share the same class at runtime." >> >> > 2> >> >> See also, Bloch, ch. 5: >> >> > > It's subtler than that. The generic parameter is on 'Foo', not the map. > The map type does not depend on the type parameter. So the question is > why the 'get()' returns 'Object'. Naively, one would expect the type of > 'bar' to resolve to 'Map' no matter what '' is, or > isn't. > > The only thing I can think of is that leaving the type parameter out in > the containing class means that all bets are off, and the compiler gives > up on generics throughout that variable's depth. Correct (although "gives up" is an unfair characterization). One of the things that I learned recently about generics is that using a raw type e.g. Foo foo2 = ... causes the compiler to treat everything within that type as raw (with respect to the particular variable that was declared with the raw type). That is, any use of foo will now be treated as if everything within the Foo class was defined as a raw type. Thus, private Map bar = new HashMap(); now gets treated as if it was written private Map bar = new HashMap(); Now (as far as the compiler is concerned) foo2.bar.get("x") produces an Object and not the Integer that one naively might expect. > Thus it doesn't even > bother to parse the 'bar' parameters, defaulting thus to ''. > Consequently, the type of the expression 'foo2.bar.get("x")' is > 'Object'. The assignment target of that expression is 'Integer', and > that requires an explicit downcast, omitted in the code along with the > type parameter. > > This is an object lesson (pun intended) in how bad it really can be to > omit the type parameter. > > I haven't looked up chapter and verse on this reasoning yet. Anyone > care to rise to the challenge? It better be someplace in the JLS but I have not looked. I prefer to use Angelika Langer's Java Generics FAQs. FAQ_203 says (in part): "Fields of a raw type have the type that they would have after type erasure." See That is somewhat subtle but describes the situation observed here. I have seen a clearer explanation elsewhere recently but cannot recall where. Sorry. > > A couple of observations: > > Type erasure has absolutely nothing to do with this. Type erasure > happens at run time. It will never create a compiler error. Au contraire. The Java Tutorial says "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." See I interpret this to mean that type erasure happens at compile time - not at run time. > > The direct dot reference to a 'private' member is a bit dodgy, though > allowed in the class's own 'main()'. >