• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics From John Meyer Mock (? super String)

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is from John Meyers' very cool 1.5 mock exam.

List<? super String> list = new ArrayList<String>(); //line1

I am happy with the idea of 'list' being able to be assigned an ArrayList of "anything that is a String or Supertype of String". The next line however does not compile.

list.add(new Object()); //line 2

Yet I don't understand why the line above fails.

I tried changing line 1 to:

List<? super String> list = new ArrayList<Object>();

Which compiled fine, but this still didn't allow me to add an object to the 'list' list.

So what exactly can be added to list? How do we know what can be added - and how does the compiler know? (I score poorly on generics again and again - so I suspect there is a fundemental lack of understanding on my part!)

-Tom
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


List<? super String> list = new ArrayList<String>(); //line1

I am happy with the idea of 'list' being able to be assigned an ArrayList of "anything that is a String or Supertype of String". The next line however does not compile.

list.add(new Object()); //line 2

Yet I don't understand why the line above fails.



A List<? super String> could either be a List<Object> or a List<String>. You actually don't know which it is. So, in order to add something to the list, it has to satisfy either case. In other words, you can only add String to the list, as a string can be added to either a list of objects, or a list of strings.

Henry
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I should say I did not get the answer properly

I thought,


is supposed to be allowing any object or any of string type.

Then how the following line fails,


I thought it should be working fine.

Regards,
Neo
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom,

I got your problem clearly.

Lets take different method signatures...
1) public void addAnyThingToList(List l) { } //1.4 type
It can intake any List<String> or ArrayList<Employee> and add any object to it. We have no complile time check for Type of List. So compiler warns us.

2) public void displayAnyList(List<?> l) { }
List<?> == List<? extends Object>
So it can take any List; But it can't perform add of any type.

3) public void displayStringsOnly(List<? extends String> l) { }

it can only take list<String> or ArrayList<String>
As String is final <? extends String> seems funny. But it is useful when you don't want to perform any add operation in that method.

4) public void addStringsOnly(List<? super String> l)

it can intake any List with type String or any super class.
As "public final class String extends Object". we can only pass, List<Object> (or) ArrayList<Object> (or)
List<String> (or) ArrayList<String>.

"But you can only add Strings."


Hope it is clear by now.
 
Neo Phesus
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds very clear

Thanks a lot...
 
Tom Scott
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Henry has explained it to me.

But to check..

It seems to me we have two set of rules.

One rule relates to what the variable can be assigned to which as I said I was happy with.

The other part is what can actually be added to a list referenced by a variable of type List<? super String>.

As per Henry's explanation I think the general rule is we can only add things that can be added legitimately to all kinds of lists of Strings or super types of Strings.

The following seems to help make things clear to me.



Many thanks all.
-Tom
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom,

I am happy to see you are already aware this.
But i want to make sure things are clear.
Given>
public static void main(String[] args) {
List<? super Number> list = new ArrayList<Number>(); //ok

list.add((Number)new Integer(10)); // ok
list.add(new Integer(20)); // ok
list.add(new Object()); // Does not compile
list = new ArrayList<Integer>(); // Does not compile
}
1> List<? super Number> list = new ArrayList<Number>(); //ok
Means you can add any Object to list which passes "IS-A Number" test.

2> List<? super Number> list = new ArrayList<Integer>(); fails because
Integer is not super class of Number.

If you know this already. I am happy.


[ March 05, 2007: Message edited by: Srinivasan thoyyeti ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It appears I am 5 posts late

It looks like the others have contributed very good explanations. Thanks for your kind words Tom.
[ March 05, 2007: Message edited by: John Meyers ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic