Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Wildcards and assignment

 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading about generics in below link

Wild Cards

according to link above



above code compiles fine, the answer to it from the link is as below

"List of Integer types can be treated as some Collection of Number types through the wildcard instantiation. If you think about it, you'll see that there is no conflict here. A List is certainly a Collection. And all we're doing is widening the type by which we can read the elements from Integer to Number".

if so why below code does not compile



How wildcards can make difference here.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics types must be the same. Period. You can't do this:

List<Number> = new ArrayList<Integer>();

It only works with

List <Number> = new ArrayList<Number>();
or
List<Integer> = new ArrayList<Integer>();

It might be strange because you CAN do it with arrays:

Number[] num = new Integer[5];

Well, the explanation is a little long, but short: Generics verification only occurs at compiling time. At runtime, java use these classes as if they were no generic restriction. Array does use types at runtime.

So, why is it important?

What happens if I do this?

List <Number> = new ArrayList<Integer>;

List.add(new Float(2.1));

See what happened? It should be legal, but it is wrong. At runtime, java won't notice it when you add it, but it will explode when you retrieve it.

When you use wildcard using extends, you're saying that you won't add anything to the list, so you can assign it. By the way, you can add items if you're using wildcard with super, just think about why.

Test what happens using arrays:

Number [] num = new Integer[4];
num[0] = new Float (2.1);

It will explode at runtime!

[ May 24, 2008: Message edited by: Daniel Del Moral ]
[ May 24, 2008: Message edited by: Daniel Del Moral ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daniel

thanks for your fast and clear reply.

I read K & B book before from that i understood that polymorphic assignment applies to collection type or base type not to generic type.

but i was surprised to see that it is not same with wildcards,now i understood from your explanation that just because i am using extends it is allowing me to compile.

i tried with super it is giving me compile time error


[ May 24, 2008: Message edited by: ramesh maredu ]
 
Daniel Del Moral
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because Integer is not a superclass of Number. Try this:

List<Number> listSuperIntegers=new ArrayList<Number>();
Collection<? super Integer> collectionSuperNumbers=new ArrayList<Number>();
collectionSuperNumbers=listSuperIntegers;
 
Daniel Del Moral
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also try:

List<? super Number> listSuperIntegers=new ArrayList<Number>();
Collection<? super Integer> collectionSuperNumbers=new ArrayList<Number>();
collectionSuperNumbers=listSuperIntegers;
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the example i gave with super is not compiling because


Collection<? super Number> can take Number or super types of Number but it can not take Integer.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramesh..i am preparing for SCJP 1.5.
 
Santosh Arun Kumar Gudipati
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramesh..i am also preparing SCJP 1.5
I want your advice on this regarding what are the inportant topics to be covered that appear mostly in the exam.
I will also appreciate if you can provide me with good URL's and e-books.
My gmail id is crazysantu@gmail.com.
Thanks in advance.
 
What a show! What atmosphere! What fun! What a tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic