Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Why not compile?

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

import java.util.*;
class test {
public static void main(String[] args) {
List<? super Integer> list = new ArrayList <Object> ();
list.add(new Object());
list.add(23);
list.add(23.0);
}
}
Why not compile?

 
Sheriff
Posts: 9691
42
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
I think its because of list.add(23.0); as it will be wrapped up into a Float object which is not a sub-class of Integer....

Please correct me as I am very weak at Generics...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankit Garg:
I think its because of list.add(23.0); as it will be wrapped up into a Float object which is not a sub-class of Integer....

Please correct me as I am very weak at Generics...



correct

then wat aboout list.add(new Object())?

Hope This Helps
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By using "List<? super Integer>" in our type declaration of list, we include only elements who are either of type Integer or elements for whom Integer is super class.
 
Ankit Garg
Sheriff
Posts: 9691
42
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 seetharaman venkatasamy:


then wat aboout list.add(new Object())?



I also thought that but I didn't say it as I didn't wanted to sound really stupid if I was wrong....
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Varinder,

The code will not compile as it is declared
<code>
List<? super Integer> list
</code>

when calling list.add(new Object())
so compiler is unsure about which super class of Integer will be added.
So compiler restricts you from adding. However you can add an Integer only.
<code>
List<? super MyClass> list
</code>

you can add object of MyClass or any of its subclass
Hope this ll give you a little better idea.

Thanks,
Manoj Mishra
SCJP1.5 97% on yesterday
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by varinder mahajan:
import java.util.*;
class test {
public static void main(String[] args) {
List<? super Integer> list = new ArrayList <Object> ();
list.add(new Object());
list.add(23);
list.add(23.0);
}
}
Why not compile?
[QB][/QB]



Hi!

List<? super Integer> list declares a List of an (almost) unknown type. The only thing you know about that unknown type is that its an Integer or a supertype of an Integer.
Once the List is created at runtime , it will, like all Lists, accept elements of that (almost unknown at compile time)type and its SUBTYPES.
How can the compiler know for sure this will happen ?
By not allowing you insert elements that are supertypes of Integer.
That way, no matter the type of List you create at runtime(Integer or supertype of it) its guarentted they are all subtypes of the type of List created.

Hope it helps

Fernando
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic