aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

saravana.T kumar
Greenhorn

Joined: Jul 13, 2007
Posts: 23
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

Joined: Feb 28, 2007
Posts: 1707


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 ]

cmbhatt
kishore Kumar Sangam
Ranch Hand

Joined: Jul 03, 2007
Posts: 34
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Apr 21, 2007
Posts: 100
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics