Return type of an interface with a wildcard

Hi everyone,

I'm totally new to generics and I had encountered a problem with instantiating

a generic array. I know, that it is impossible to do that and I found a workaround:

<T> List<T>[] newListArray(int size){

return (List<T>[])new List<?>[size];

}

This method returns and array of a generic interfaces and works fine.

The only problem is that I'm not sure how it works ...

Could anybody correct me if I'm wrong:

- the method instantiate an array of interfaces of any type and then

it sees that this any type is cast to a T, which in this case is String ? :

List<String>[] t=b.newListArray(3);

I really got confused here. Could anybody with a good how-it-works-explanation

attitude tell me how it works ?

Thanks,

Adrian

[1006 byte] By [AdrianSosialuka] at [2007-11-27 1:59:12]
# 1
What's the difference to simply returning a raw List[size]? You will get an unchecked cast anyway. At runtime, it does not matter because of erasure.
stefan.schulza at 2007-7-12 1:36:43 > top of Java-index,Core,Core APIs...
# 2
Hi,Well, the difference is that I need to know how it works. That was my question anyway.I know that I can return List[], but that was not my issue.Thanks,Adrian
AdrianSosialuka at 2007-7-12 1:36:43 > top of Java-index,Core,Core APIs...
# 3

So what is your issue then? I just told you, it's the same at runtime. And that's why it works. :)

Ok, some more info: there is no cast from "unknown" to String involved. You never created an array on other than on a List of Objects and you never use another array than on a List of Objects. Generics is a compile-time-feature (at least for now).

Message was edited by: stefan.schulz

stefan.schulza at 2007-7-12 1:36:43 > top of Java-index,Core,Core APIs...
# 4

OK. Let me try to say it in other words :)

I don't understand how this code works. This

is my issue. I'm not interested in any alternatives,

so maybe if I had asked:

"How does the compiler interpret this code ?"

that would be of more usefulness ;)

Imagine someone who is trying to learn a new language,

and he/she encounters some code which he/she doesn't

understand - can be annoying ...

So basically, I need to know how the compiler interpret

the code, so I will know how to use this syntax (regardless

of its usefulness). And yes - I know that it is ONLY compile-time

type safety. But you still need to understand how it works

in order to use it properly.

Thanks,

Adrian

AdrianSosialuka at 2007-7-12 1:36:43 > top of Java-index,Core,Core APIs...
# 5

<T>

This is a type parameter.

List<T>[]

This tells the compiler that the method returns an array of List<T> objects.

newListArray(int size) {

This is the name of the method and its parameter.

return

This is a return statement.

(List<T>[])

Here you cast something to an array of List<T> objects.

new List<?>[size];

Here you create an array of List<?> objects.

Okay, that's how the compiler interprets the code. I think people are confused by your question, because that's the natural way to break it down even if you aren't a compiler. It would help if you produced a specific question about the code; "Duh" is such a hard question to answer.

DrClapa at 2007-7-12 1:36:43 > top of Java-index,Core,Core APIs...
# 6

Hi,

Now that is what I needed !

So to be more specific:

new List<?>[size];

I thought you can't use wildcards in constructors.

But I think I know where it is heading for: you can't

use wildcards in constructors but you aren't actually using

any here - am I right ? You are creating an array, not

an object (though array IS and object ...)

Cheers,

Adrian

AdrianSosialuka at 2007-7-12 1:36:44 > top of Java-index,Core,Core APIs...
# 7
Yes, you are creating an array but you are not creating any List objects.The class of the array is List<?> so an element of the array can contain any List<T> object for any value of T.
DrClapa at 2007-7-12 1:36:44 > top of Java-index,Core,Core APIs...