• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use of ? in generics instantiation

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Could anyone explain me why line 1 does not give error in this code? I thought it should.

[Based on a question from Niko's Blog - generics]
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The more interesting question is why line two gives an error.

You can specify an unknown type with an unbounded wildcard, which simply looks like <?>. An unbounded wildcard is essentially the same as saying <? extends Object>.


That would seem to allow both lines to compile.

You cannot, however, have a generic CONSTRUCTOR with a wildcard type argument. just remember "No Wildcards in constructors". Constructors of generic collections need concrete classes.

The whole concept of generics lives only in the compiler.
When you compile, the compiler makes sure your code is following the type safety rules you established on the field a2, which in line 2, you said "any object at all" on the left hand side of the equals.
One the right hand side, however, the compiler is trying to determine if the thing meets the requirements you established on the left hand side, and it can't.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim McGuire wrote:One the right hand side, however, the compiler is trying to determine if the thing meets the requirements you established on the left hand side, and it can't.



Thanks Tim, but the same applies to line 1. Why does the compiler let you get away with "new ArrayList<Set><?>>()". . I thought that the righ-hand side could never contain a '?'
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no wildcard in a constructor on the right hand side in line 1

the argument as far as the constructor is concerned is a Set. Your compiler will see that you are guaranteed to make an ArrayList containing only Sets and that is all it cares about here.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Your compiler will see that you are guaranteed to make an ArrayList containing only Sets and that is all it cares about here.





Thanks a lot, Tim!! It is clear to me now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic