aspose file tools*
The moose likes Beginning Java and the fly likes Whether code will compile or not..?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Whether code will compile or not..??" Watch "Whether code will compile or not..??" New topic
Author

Whether code will compile or not..??

nans nav
Greenhorn

Joined: Jun 07, 2007
Posts: 8
public static void main (String[] arguments) {
//call 1
List<? super Object> ls1 = new LinkedList<Object>();addandDisp(ls1,new String());
//call 2
List<? extends Object> ls2 = new LinkedList<Object>();addandDisp(ls2,new Object());
//call 3
List<Object> ls3 = new LinkedList<Object>();addandDisp(ls3,new String());
//call 4
List<? super Object> ls4 = new LinkedList<Object>();addandDisp(ls4,new Object());
}
public static <T> void addandDisp(Collection<T> cs, T t) {}

Which call will allow the ode to complie without error..??? and why..??


SCJP 5.0<br />SCWCD 5.0
Charles McGuire
Ranch Hand

Joined: Jan 18, 2005
Posts: 99
You know, when I use Eclipse I get the feedback to "does this compile" type questions immediately. It gives me the message as I type since it compiles on the fly. Sometimes it even suggests remedies. I bet other IDE's do this too.

That way, you can narrow your problem down to a specific question without exhausting the good people on these boards.


There's no place like 127.0.0.1
nans nav
Greenhorn

Joined: Jun 07, 2007
Posts: 8
Could you tell me why it is not able to compile..? why call 1 and call 4 are able to compile since there is super of class Object. and why call 2 is not compiling since every class extends object class.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
This has the distinct look of a homework or interview question, so I shan't give a complete answer. However...

This type of code will compile if the declared type of the object reference is compatible with the type of object that you are trying to assign to it. In the case of generic collections, it is necessary that any object that could legally be added to the object you actually created (with "new") is also a legal member of the declared type of collection.

So, for instance, if the declared type was a List of String, but you actually created a List of Object, it would not compile, because it is legal to put things into a List of Object that would not be legal in a List of String.

Also, bear in mind that a List of Object is not a superclass of a List of String, even though Object is a superclass of String. This is the problem for which the "? extends A" and "? super B" syntax exists. A List of "? extends Object" IS a superclass of a List of String.

Lastly, can you (or anyone) think of any real use for "? super Object"? I can't!


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Peter Chase:
Lastly, can you (or anyone) think of any real use for "? super Object"? I can't!


When I wrote that Socket wrapper I would have liked to call:

return new super(in-val, in-val);

for get Instance if the values supplied to the wrapper class were within range.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Whether code will compile or not..??