• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Whether code will compile or not..??

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..??
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
nans nav
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic