• 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

Generic bounded assignment

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

what is the rule for assignment of <? super > & <? extends>?

Queue<? extends Number> q1 = null;
Queue<? super Integer> q2 = null;

q1 = q2; //What should happen here?

q2 = q1; //What should happen here?

I am quite confuse about generic assignment...can anybody explain me the rule for generic bounded assignment ???

Thanks in advance.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nilu! Welcome to javaranch

Well Nilu you will have to understand that generics is just a feature to save you from adding wrong elements into a collection.

Queue<? extends Number> q1 = null;

will define that this queue will contain objects which are sub types of Number. So you cannot add objects to this queue as you don't know what the actual type of the objects in the queue will be. But you can retrieve objects from this queue into references of type Number

Number n = q1.peek();

This is because you are sure that the objects in the queue are of sub-type of Number.

Now coming to the second case

Queue<? super Integer> q2 = null;

This queue will contain elements which are a super type of Integer. It can be any super type at any level i.e. super type of super type of Integer and so on. So you cannot retrieve objects from it as you don't know what the actual type of objects will be(you can however retrieve objects from it into references of Object class as it is the super class of every class). You can however add elements to it of type Integer and it's sub types (although Integer is a final class but it is just an example).

The two assignments that you gave will not compile which to be frank I don't know why.

Just go trough Sun Generics Tutorial and Java Generics FAQ for details...
 
Nilu Ran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks aniket for your reply...

But I am still didn't get any clue...about assignment.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nilu Ran:
Thanks aniket for your reply...

But I am still didn't get any clue...about assignment.



First of all I am Ankit not Aniket

Secondly both the assignments are giving errors. I was also shocked looking at the error. I thought that compiler would give a warning. I think the second link that I gave you might help you as it contains these type of exceptional cases.

Also I don't think these type of typical questions will come on the exam. Just read the generics tutorial by sun that I gave a link to and you will be fine for the exam...
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KS & BB SCJP 1.5 book explains generics very beautifully. Referring to the same the following assignments are valid:

LinkedList <? super Integer> l1 = new LinkedList <Integer>();
//l1 can accept a generic type which is an Integer or
//superclass of Integer. Moreover i can add an object to list l1

LinkedList<? extends Number> l2 = new LinkedList <Integer>();
//l2 can accept a generic type which is an Number or
//subclass of Number. Moreover i can NOT add an object to list l2

LinkedList <Number> l3 = new LinkedList <Number>();
//l3 can accept a generic type only of type Number. i can add to
//this list

The following are invalid assignments:

LinkedList <? extends Number> l4 = new LinkedList<? extends Number>();
//I cannot use wildcard notation while creating an object as it is
//allowed to be used only for reference declarations

Quoting KS && BB "Keep in mind that the wildcards can be used only for reference declarations(including arguments, variables, return types, and so on). They can't be used as the type parameter when you create a new typed collection. Think about that�while a reference can be abstract and polymorphic, the actual object created must be of a specific type. You have to lock down the type when you make the object using new."

Moreover, generic types are verified by the compiler by a process called "type erasure" hence the generic type information is removed after verification at compile time.

so based on the facts above I guess the compiler does the following:

Queue<? extends Number> q1 = null;
Queue<? super Integer> q2 = null;

q1 = q2; //What should happen here?
//--> The compiler resolves the generic type of reference variable
//--> q2 to be containing wild card characters hence throws a
//-->compile time exception.

q2 = q1 //What should happen here?
//--> Same as above



So if you say

Queue<? extends Number> q1 = null;
Queue<Integer> q2 = null;

q1 = q2; //This will compile as the compiler is able to lock the type on
//the right hand side of the assignment operator.

q2 = q1; //Will not compile as the compiler is NOT able to lock the type

Hence the compiler makes sure that when an object or object reference is assigned to a generic typed reference variable the reference variable on the right hand side should have a generic type not containing wild card notation.

i hope this helps. Any ranch hands please correct me if i am wrong.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice one Harvinder. Actually I read generics from K&B but couldn't understand anything so read it from sun's tutorial and this is not given in that. I don't remember what's given in K&B...
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Harvinder's post quoted from K&B
The following are invalid assignments:
LinkedList <? extends Number> l4 = new LinkedList<? extends Number>();
//I cannot use wildcard notation while creating an object as it is
//allowed to be used only for reference declarations

While going thru the Sun tutorial on Generics (link provided by Ankit) I came across the following on page 9:
static List<List<? extends Shape>> history = new ArrayList<List<? extends Shape>>();

So I am wondering if this is allowed in some cases? Or am I missing something here?

Thanks,
Meera
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Meera but the example you gave is valid as actually there is no actual object being created which has a wild card.

new ArrayList<List<? extends Shape>>();

Here you are saying that this ArrayList will contain Lists. The type of the lists that this ArrayList will contain can be any sub-type of Shape. So basically you are not instantiating any object with a wild card. You are just saying that this ArrayList will contain some lists whose type uses a wild card.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic