• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A generics question

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In the Arrays Class, there's this method :

public static <T,U> T[] copyOf(U[] original,
int newLength,
Class<? extends T[]> newType)

I can't understand <? extends T[]>. What does it mean "An array that extends an array of an unknown type"? Does this not amount to any array?

Why the following wouldn't have be correct?

public static <T,U> T[] copyOf(U[] original,
int newLength,
Class<T[]> newType)


Thanks.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marwen Bakkar wrote:I can't understand <? extends T[]>. What does it mean "An array that extends an array of an unknown type"? Does this not amount to any array?


It doesn't mean that at all. The type "T" is not "unknown" at all, it's specified each time you call the method and specify the type parameters T and U.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes but how is it specified? It's inferred from the argument. So you get back the type you pass in. Unless it's meant for something like :
Integer[] original = ...
Number[] b = ...
b = copyOf<Number,Integer)>(originalArray, n, Double.class);

But I don't see it's use?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that because of type erasure, it's impossible to create arrays of a generic type without a Class object.

As for the way you called it, the third parameter should be Double[].class, not Double.class. The method could have worked with Double.class but Sun decided to want an array class instead of the element class.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
As for the way you called it, the third parameter should be Double[].class, not Double.class. The method could have worked with Double.class but Sun decided to want an array class instead of the element class.


You're right.

I understand the need to pass in a Class parameter. What I don't understand is the declaration. Why Class<? extends T[]> and not Class<T[]>?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've checked the source code of that method (from src.zip inside the JDK folder), and it uses the Class object to create the array. Therefore, in your example the returned array is a Double[]. I think they chose the ? extends to give it more flexibility. You can create a Double[] but use a Number[] or Object[] reference for it. I can't think of a scenario where you would want this though.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me neither... hence the question. Thanks for your input!
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic