Forums Register Login

A Generic Question

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
What is the compile error message?
+Pie Number of slices to send: Send
This is because you cant add to lists when you are using the ? notation .
This is similar to the foll case :

List<Integer> temp = new ArrayList<Integer>();
doSomething(temp);

public static void doSomething(List<? extends Object> x){
x.add(new Object()); //Error ... U cant add to list ..
}

The point is when you are using the ? (wildcard) notation, you enter an obligation tht you wont add anything into the list .
Hope this makes it clear.
+Pie Number of slices to send: Send
That is not true puneet.
There's nothing wrong with the list.add(23)
(as 23 is boxed into an Integer).
The problem is adding Objects or Doubles to the list.
[ August 16, 2008: Message edited by: John Sutt ]
+Pie Number of slices to send: Send
 

Originally posted by Ross Zen:
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?



I think following program will give you something............
+Pie Number of slices to send: Send
THe error is adding new Object() and a double 23.0

I just don't know why...


Double , I understand is not a super class of Integer, but what about Object?
+Pie Number of slices to send: Send
Thanks Guys,after rereading K&B`s book,here is what i think about the question:
We use generic mostly on collection ,then we can define the type of the collection,Generic wont let us assign a reference which is not the exactly the type of the collection we declared,Because type information is erased when running,it only works during compile time.Second,we don�t wont a function adds other types into the collection.BTW be careful that we are talking about type safe collection reference here(when being assgined or as an argument).We still can add things to an type safe collection when it pass a IS-A test.How about the Polymorphism? Wildcard is used to slove this.
We use <? extend XX> or <?>but we don�t add thing to the collection,we use <? Super XX> we add limitd things.
+Pie Number of slices to send: Send
Sorry.. .was confused a bit..
Actually you can add to Lists when you use the ? notation and tht case is when using super.
Eg: List<? super Integer> temp = new ArrayList<Integer>();

But the only thing you can add to the List temp is an Integer object and nothing else. Not a super type , not a sub type. Has to be an Integer. Tht explains why you are getting an error message when you try to add new Object().
+Pie Number of slices to send: Send
Why don't you try running this code:

class Base{

}
class Derived extends Base{

}

public class Test{
public static void main(String[] args){
List<? super Base> list = new ArrayList<Base>();
list.add(new Derived());
}
}

to see if it compiles?

Imho, the best way to learn is to experiment through lots of code yourself.
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1076 times.
Similar Threads
Why not compile?
one more doubt abt generics
Genrecis and Wildcard
Generic types
GENERICS
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 10:02:16.