AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Strangely working java generics.

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java: Support and criticism
View previous topic :: View next topic  
Author Message
minkoo.seo@gmail.com
Guest





PostPosted: Sun Sep 11, 2005 4:30 pm    Post subject: Strangely working java generics. Reply with quote



See the following code:


class A {}

class B {}

public class Test {

public static void main(String[] args) {
A a = null;
foo(a);
}

public static <T> void foo(T a) {

T[] t = (T[]) new Object[10];
Object[] o = t; // correct. Object[] is parent of T[].
o[0] = new B(); // correct. Insert B into object[].
T val = t[0]; // Type error if T is not A!!

System.out.println(t[0]);
}
}



T[] t = (T[]) new Object[10]; is the way of declaring an array of T
that people in this forum recommend. However, I've found something
quite strange. As you can see in the code above , T must be A. However,
strangely enough, T val = t[0] succeeded. In this code, T is obviously
A, and t[0] is also obviously B. However, JDK raises only one warning
on T[] t = (T[]) new Object[10], saying that "cast from Object[] to T[]
is actually checking against erased type Object[]."

Though the tutorial on generics says that codes are type-safe only when
there's no warning, assigning instance of B into the variable whose
type is A is quite weird.

Is this a bug or my mistake?

Sincerely,
Minkoo Seo

Back to top
Roedy Green
Guest





PostPosted: Mon Sep 12, 2005 3:32 am    Post subject: Re: Strangely working java generics. Reply with quote



On 11 Sep 2005 09:30:42 -0700, [email]minkoo.seo (AT) gmail (DOT) com[/email] wrote or quoted :

Quote:
T[] t = (T[]) new Object[10];

I think the key is that cast is bogus. You can't cast Object[] to T[]
even if all the element in the array are T. You can only do it by
creating a T[] array and copying the elements over.

But from Java' Alice in wonderland view, T[] t isn't really a T[]
after all. it is an Object[] so it doesn't matter that the cast makes
no sense.

I grit my teeth at this stuff. Sometimes type checking is all make
believe.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Back to top
minkoo.seo@gmail.com
Guest





PostPosted: Mon Sep 12, 2005 8:23 am    Post subject: Re: Strangely working java generics. Reply with quote



I'm sorry, but I don't understand what you are trying to say.

Roedy Green wrote:
Quote:
But from Java' Alice in wonderland view, T[] t isn't really a T[]
after all. it is an Object[] so it doesn't matter that the cast makes
no sense.

So, do you think that allocating B into the array whose type is A is
okay in this example because the actual storage is declared as
Object[]? I don't think so. What matters here is that the javac behaves
contrary to the intuition.

p.s.
I know that the example I've shown is quite weird. I've made it based
on the codes in Section 7.3, java generics tutorial
([url]http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf)[/url].

Sincerely,
Minkoo Seo


Back to top
Oliver Wong
Guest





PostPosted: Mon Sep 12, 2005 3:17 pm    Post subject: Re: Strangely working java generics. Reply with quote

<minkoo.seo (AT) gmail (DOT) com> wrote

Quote:
See the following code:


class A {}

class B {}

public class Test {

public static void main(String[] args) {
A a = null;
foo(a);
}

public static <T> void foo(T a) {

T[] t = (T[]) new Object[10];
Object[] o = t; // correct. Object[] is parent of T[].
o[0] = new B(); // correct. Insert B into object[].
T val = t[0]; // Type error if T is not A!!

System.out.println(t[0]);
}
}



T[] t = (T[]) new Object[10]; is the way of declaring an array of T
that people in this forum recommend. However, I've found something
quite strange. As you can see in the code above , T must be A. However,
strangely enough, T val = t[0] succeeded. In this code, T is obviously
A, and t[0] is also obviously B. However, JDK raises only one warning
on T[] t = (T[]) new Object[10], saying that "cast from Object[] to T[]
is actually checking against erased type Object[]."

Though the tutorial on generics says that codes are type-safe only when
there's no warning, assigning instance of B into the variable whose
type is A is quite weird.

Is this a bug or my mistake?

This has been addressed before in the newsgroup. The short answer is,
because of backwards compatibility reasons, Java's generics is not a nice as
we'd like it to be.

"T[] t = (T[]) new Object[10];" is not legal when T refers to A. That is
to say, and array of A is not the same as an array of Object.

- Oliver



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java: Support and criticism All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.