GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why not compile? 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 "Why not compile?" Watch "Why not compile?" New topic
Author

Why not compile?

varinder mahajan
Ranch Hand

Joined: Jun 11, 2008
Posts: 47
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?


Beat the world,if you can.......
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 01, 2007
Posts: 20
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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

Joined: Aug 18, 2008
Posts: 5
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

Joined: Aug 19, 2008
Posts: 1
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.
 
subject: Why not compile?