This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A Generic Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A Generic Question" Watch "A Generic Question" New topic
Author

A Generic Question

Ross Zen
Greenhorn

Joined: Aug 16, 2008
Posts: 2
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?
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2053
What is the compile error message?
puneet agrawal
Greenhorn

Joined: Aug 16, 2008
Posts: 2
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.
John Sutt
Ranch Hand

Joined: Mar 07, 2008
Posts: 42
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 ]

SCJP 6 (91%), SCJD (91%)
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
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............


SCJP 5
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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?


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Ross Zen
Greenhorn

Joined: Aug 16, 2008
Posts: 2
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.
puneet agrawal
Greenhorn

Joined: Aug 16, 2008
Posts: 2
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().
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Generic Question