• 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

Generics

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic