In implementation of a generic stack ,the following idiom is used and works without any problem
在通用堆栈的实现中,使用以下习语并且没有任何问题地工作
public class GenericStack<Item> {
private int N;
private Item[] data;
public GenericStack(int sz) {
super();
data = (Item[]) new Object[sz];
}
...
}
However when I try the following ,it causes a ClassCastException
但是,当我尝试以下操作时,它会导致ClassCastException
String[] stra = (String[]) new Object[4];
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
How do you explain this?
你怎么解释这个?
4 个解决方案
#1
3
Casting a new Object[4]
to a String[]
doesn't work because an Object[]
isn't a String[]
, just like an Object
isn't a String
.
将新的Object [4]转换为String []不起作用,因为Object []不是String [],就像Object不是String一样。
The first example works because of type erasure. At runtime, the type parameterItem
has been erased to Object
. However it would similarly fail if you tried to assign the array to a reifiable type, for example if data
wasn't private
:
第一个例子因类型擦除而起作用。在运行时,类型parameterItem已被擦除为Object。但是,如果您尝试将数组分配给可重新生成的类型,例如,如果数据不是私有的,则同样会失败:
String[] strings = new GenericStack<String>(42).data;
This would similarly throw a ClassCastException
, because what is actually an Object[]
would be cast to String[]
.
这同样会引发ClassCastException,因为实际上Object []将被强制转换为String []。
#2
2
Because of generics type erasure Item array becomes Object array effectivelly. Therefore type matches. But when you do it with concrete type String it does not. No type erasure is applied and it fails.
由于泛型类型擦除,Item数组有效地成为Object数组。因此键入匹配。但是当你用混凝土类型String做它时它不会。没有应用类型擦除,它失败了。
#3
1
I think if a Object class reference is pointing to any child class object like String class or any other class then only we can cast the object class reference to that particular class (which object we have created and assigned to Object class ref) and assign a reference to it. and if we create a direct object of Object class (as you have mentioned) will not work.
我认为如果Object类引用指向任何子类对象,如String类或任何其他类,那么我们只能将对象类引用转换为该特定类(我们创建并分配给Object类ref的对象)并分配参考它。如果我们创建一个Object类的直接对象(正如你所提到的)将无法工作。
Example:
Object[] obj = new String[4];
String[] stra = (String[]) obj;
above code will not generate any ClassCastException.
上面的代码不会生成任何ClassCastException。
Hope this helps
希望这可以帮助
#4
0
I think this is the best answer to this question:
我认为这是这个问题的最佳答案:
"A String[] is not an Object[], which is a little counter-intuitive. You're encountering the "Banana is-a fruit" but a "List Of Bananas is-not-a List Of Fruit" scenario."
“一个字符串[]不是一个对象[],这有点违反直觉。你遇到的”香蕉是一种水果“,但是”香蕉列表不是水果列表“的情景。”
Ref: https://*.com/a/1018774/847818
From the same question, how to cast:
从同一个问题,如何投射:
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
or
String[] stringArray = Arrays.asList(objectArray).toArray(new String[objectArray.length]);
#1
3
Casting a new Object[4]
to a String[]
doesn't work because an Object[]
isn't a String[]
, just like an Object
isn't a String
.
将新的Object [4]转换为String []不起作用,因为Object []不是String [],就像Object不是String一样。
The first example works because of type erasure. At runtime, the type parameterItem
has been erased to Object
. However it would similarly fail if you tried to assign the array to a reifiable type, for example if data
wasn't private
:
第一个例子因类型擦除而起作用。在运行时,类型parameterItem已被擦除为Object。但是,如果您尝试将数组分配给可重新生成的类型,例如,如果数据不是私有的,则同样会失败:
String[] strings = new GenericStack<String>(42).data;
This would similarly throw a ClassCastException
, because what is actually an Object[]
would be cast to String[]
.
这同样会引发ClassCastException,因为实际上Object []将被强制转换为String []。
#2
2
Because of generics type erasure Item array becomes Object array effectivelly. Therefore type matches. But when you do it with concrete type String it does not. No type erasure is applied and it fails.
由于泛型类型擦除,Item数组有效地成为Object数组。因此键入匹配。但是当你用混凝土类型String做它时它不会。没有应用类型擦除,它失败了。
#3
1
I think if a Object class reference is pointing to any child class object like String class or any other class then only we can cast the object class reference to that particular class (which object we have created and assigned to Object class ref) and assign a reference to it. and if we create a direct object of Object class (as you have mentioned) will not work.
我认为如果Object类引用指向任何子类对象,如String类或任何其他类,那么我们只能将对象类引用转换为该特定类(我们创建并分配给Object类ref的对象)并分配参考它。如果我们创建一个Object类的直接对象(正如你所提到的)将无法工作。
Example:
Object[] obj = new String[4];
String[] stra = (String[]) obj;
above code will not generate any ClassCastException.
上面的代码不会生成任何ClassCastException。
Hope this helps
希望这可以帮助
#4
0
I think this is the best answer to this question:
我认为这是这个问题的最佳答案:
"A String[] is not an Object[], which is a little counter-intuitive. You're encountering the "Banana is-a fruit" but a "List Of Bananas is-not-a List Of Fruit" scenario."
“一个字符串[]不是一个对象[],这有点违反直觉。你遇到的”香蕉是一种水果“,但是”香蕉列表不是水果列表“的情景。”
Ref: https://*.com/a/1018774/847818
From the same question, how to cast:
从同一个问题,如何投射:
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
or
String[] stringArray = Arrays.asList(objectArray).toArray(new String[objectArray.length]);