Why does undefined Generics types of Collections defaults to Object even if bound to other class?

class Test<G extends String>{ public G test(){return null;} public List<G> tests(){return new ArrayList<>();} } public void doTest(Test t){ //works fine String str = t.test(); //Compile error: expected String found Object str = t.tests().iterator().next(); }

I would like the last line to return a String instance instead of Object, as the type G was bound to subclass String. There is any other way than casting ?


No, the type as been erased. You're running into two different rules for type erasure - from JLS section 4.6:

    <li>The erasure of a parameterized type (§4.5) G is |G|.</li> <li>The erasure of a type variable (§4.4) is the erasure of its leftmost bound.</li> </ul>

    So the erasure of List<G> is List, but the erasure of G is String - that's why the first assignment works.

    All you need to do to get this code to compile is use a wildcard in your parameter:

    public void doTest(Test<?> t)


  • Why this macro accepts a template with 1 parameter and refuses a template with 2 parameters?
  • RegEx help to remove noise words or stop words from string
  • check if numbers have the same sign
  • Sorting a HashMap, while keeping duplicates
  • Simplify where clause with repeated associated type restrictions
  • (Play 2.5) How do you define json format for type alias of an Option?
  • @tailrec why does this method not compile with 'contains a recursive call not in tail position&
  • Mercurial hook to set policy on tag names
  • Building jamvm 1.5.4 on OS X Lion
  • How do I import an existing Grails 3 (3.0.12) project in IntelliJ 15
  • drawing random circles, storing their coorindates in an array
  • C++ friend class std::vector
  • Using $compile in a directive triggers AngularJS infinite digest error
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • Struts 2 TextField Tag with an attribute and no value
  • NUnit 3.0 TestCase const custom object arguments
  • Groovy: Unexpected token “:”
  • Functions in global context
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Read text file and split every line in MSBuild
  • Sony Xperia Z Tablet not found by adb
  • Is possible to count alias result on mysql
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Check if a string to interpolate provides expected placeholders
  • ILMerge & Keep Assembly Name
  • How to handle AllServersUnavailable Exception
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • 0x202A in filename: Why?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • How do I configure my settings file to work with unit tests?
  • Qt: Run a script BEFORE make
  • Is it possible to post an object from jquery to bottle.py?
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How do I use LINQ to get all the Items that have a particular SubItem?