• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
saravana.T kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
import java.util.*;
public class test
{
public static void main(String args[])
{
List<Integer> l=new ArrayList<Integer>();
new test().hi(new ArrayList<List<? extends Number>>());
}
public void hi(List<List<? extends Number>> a)
{
a.add(new ArrayList<Integer>());
}
}
I have a doubt regarding generics , How can the above code compile
because it adds an element to <? extends Number>

import java.util.*;
public class test
{

public static void main(String args[])
{
List<Integer> l=new ArrayList<Integer>();
l.add(5);
List<Integer> q=new ArrayList<Integer>();
l.add(1);
List<List<? extends Number>> a=new ArrayList<List<? extends Number>>();
a.add(l);
a.add(q);

for(List<? extends Number> o:a)
System.out.println(o);
}
}
This example is same as the above one as you can see it adds the eleement

Please explain why does it add when it says ? (wildcards) are not supposed to make an type be added
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You are adding a List to the a not a Number object to the List.
You are right that we can't add to the <? extends Number> because
it is read only. But you see it is, List<List<? extends Number>>.

Treat it like:


Thanks,
[ August 05, 2007: Message edited by: Chandra Bhatt ]
 
kishore Kumar Sangam
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dude,
The following points might be note worthy,

1.The right hand side <Type> has to be freezed.

for eg: List <Integer> x = new ArrayList<? extends Number> // Compile Time error <? extends> and <? super> cannot be used on the right hand side until unless the first level has been freezed.

i.e in your case

List<List<? extends Number>> a=new ArrayList<List<? extends Number>>();


The above code is trying to instantiate an ArrayList that holds A List of Objects that are sub-classes of Number or a Number Class. In this code snippet.

The First level is ArrayList(List) -- This Type has to be freezed at compile time.

The second Level is ArrayList(List(? extends Number))




2. ? extends - YOU CANNOT ADD ELEMNETS.

The above code is adding elements of type List at Level 1 but not ElEMNETS OF TYPE NUMBER to LEVEL 2.



3. ? super - YOU CAN ADD ELEMENTS AT YOUR RISK - COMPILER WARNINGS
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kishore Kumar Sangam:
3. ? super - YOU CAN ADD ELEMENTS AT YOUR RISK - COMPILER WARNINGS
Not correct. It is safe, to add elements of the specified type and the compiler will not issue any warnings.
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishore,

Please can you provide the link or source where did you find the below statements.
Thanks in advance.


Originally posted by kishore Kumar Sangam:
Hi Dude,
The following points might be note worthy,

1.The right hand side <Type> has to be freezed.

for eg: List <Integer> x = new ArrayList<? extends Number> // Compile Time error <? extends> and <? super> cannot be used on the right hand side until unless the first level has been freezed.

i.e in your case

List<List<? extends Number>> a=new ArrayList<List<? extends Number>>();


The above code is trying to instantiate an ArrayList that holds A List of Objects that are sub-classes of Number or a Number Class. In this code snippet.

The First level is ArrayList(List) -- This Type has to be freezed at compile time.

The second Level is ArrayList(List(? extends Number))




2. ? extends - YOU CANNOT ADD ELEMNETS.

The above code is adding elements of type List at Level 1 but not ElEMNETS OF TYPE NUMBER to LEVEL 2.



3. ? super - YOU CAN ADD ELEMENTS AT YOUR RISK - COMPILER WARNINGS
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic