• 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

Question on generic methods

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A question about generic methods from this tutorial.
http://java.sun.com/docs/books/tutorial/extra/generics/methods.html
It says that the below method can be called with any kind of collection whose element type is a supertype of the element type of the array. My question is how is this specified without using the super keyword. As per my understanding, T is a type, array and the collection are both specified to take elements of type T.



Thanks,
Devi
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example:
If I understand your question correctly, the method will add all members of the array to the collection. As long as the collection is declared with a type-compatible class of the array, the method will work.

Does this help?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm certainly no expert on generics but I'll tell you what understand by that description (and by looking at the examples in the tutorial).

Suppose X and Y are types and X is a supertype of Y (write X > Y ).
Then we know that X[] > Y[].

Suppose we have reference variables ArrayList<X> x and Y[] y, and suppose we call fromArrayToCollection(T[] a, Collection<T> c) like this:

fromArrayToCollection(y, x); //

The question is what is T, the type parameter inferred to be?

It is X, the supertype. So inside the method T is X. So the method really looks like (read the comments):

So basically this is saying that you can convert an array of Ys (which is an array of Xs) into an ArrayList of Xs.

I feel rather dizzy, so please feel free to pick holes in my interpretaion.
-Barry
[ September 23, 2005: Message edited by: Barry Gaunt ]
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry but I'm still not clear.
The method is
fromArrayToCollection(T[] a, Collection<T> c)

If you look at the implementation, then its clear that since the array elements that are added to the collection, the collection element type can
be the supertype of the array element type.

But just by looking at the declaration, doesn't it specify that the array element and Collection element are of the same type T ? Should we not say fromArrayToCollection(T[] a, Collection<? super T> c)

Thanks
Devi
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does work. But how does it work without the super keyword.
Shouldn't the method definition be as below ?

I'm really confused. Any inputs would be welcome.

Thanks
Devi
[ September 24, 2005: Message edited by: Sreedevi Vinod ]
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler makes that inference. that is how it works. I hope that answers one of your questions.
Whether it should be as you said (which is also right) or not is a question you should ask the author of the class.
As far as I can see, the original code looks more compact that the newer version you have given + does the same work, so maybe thats why it was kept.
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case you wanted to ask why this works, note that both of these have the same erasures.
both of these translate to (I think so, please correct me if this is wrong)
fromArrayToCollection(Object[] a, Collection c)
for(Object o: a)
c.add(o);
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sreedevi, if you would like me to move this thread to Java In General (Intermediate), I will do so. Perhaps you will get more help there.
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Akshay, Barry and Steve for your replies.
I think its somewhat clear now, Barry.
But still it would be helpful to receive more inputs.
So please do move it to the Intermediate forum.

Many Thanks
Devi
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will do...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sreedevi Vinod:

But just by looking at the declaration, doesn't it specify that the array element and Collection element are of the same type T?



Exactly!

The trick is that an array variable of type T[] can hold a reference to an array of type S[] as long as S is a subtype of T.

That is, if you call

fromArrayToCollection(new X[]{...}, new ArrayList<Y>());

the method gets called as

fromArrayToCollection(Y[], Collection<Y>

which just happens to work as intended.

Does that help?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja,
why is this type inference done?
Why doesn't the compiler just simply reject the call saying that the collection type parameter is not exactly the same as the array type?

That is: allow the call if parameters are Integer[], ArrayList<Integer> , but reject call if paramters are Integer[], ArrayList<Number>. The programmer has specified that he wants the former, not the latter.

As Shreedevi mentioned the programmer can specify the latter behaviour as:
T[], ArrayList<? super T> so why is the compiler being so "helpful" in the case of T[], ArrayList<T>?
[ September 25, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well - that's the idea of inheritance in java.

If a method accepts a parameter of type T (number), it will allow every subtype of T (Integer).

Having an array or a Collection of Ts doesn't make a difference.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Stefan said.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


That is: allow the call if parameters are Integer[], ArrayList<Integer> , but reject call if paramters are Integer[], ArrayList<Number>. The programmer has specified that he wants the former, not the latter.



Because the former is the latter by definition if the former is a subtype of the latter?
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:


Because the former is the latter by definition if the former is a subtype of the latter?



Ken don't be confused, Barry was citing that "it doesn't work that way" as you're thinking it does.
fromArrayToList(Integer[], ArrayList<Number> actually works and that is the whole point that is being mooted.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akshay Kiran:


Ken don't be confused, Barry was citing that "it doesn't work that way" as you're thinking it does.
fromArrayToList(Integer[], ArrayList<Number> actually works and that is the whole point that is being mooted.



Don't be confused? I have no idea what you are trying to say with this post. I know it works, Barry seemed to be asking Ija why it works and implying that it shouldn't. Are you saying he was just playing devil's advocate and I missed the sarcasm?
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hehe yeah, it wasn't clear whether you were trying to read Barry's mind or asking a seperate question altogether. I just thought I should dispel doubt (albeit nonexistent) because someone reading that would think you're making a point there sorry about that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic