File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes A generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "A generics question" Watch "A generics question" New topic
Author

A generics question

marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
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
Bartender

Joined: Oct 14, 2005
Posts: 18885
    
    8

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

Joined: Jan 28, 2010
Posts: 84
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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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

Joined: Jan 28, 2010
Posts: 84
Me neither... hence the question. Thanks for your input!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A generics question