• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why not compile?

 
varinder mahajan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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?
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
 
Abhijeet Nalawade
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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....
 
Manoj Mishra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Fernando Fortunato
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic