Java 无法从 Object 转换为 char
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23382888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot cast from Object to char
提问by user2963286
My code works perfectly as far as I can see but I still get the error "Cannot cast from Object to char" and I was wondering if anyone could explain to me what the problem is and if I can do it another way that does not cause an error.
就我所见,我的代码运行良好,但我仍然收到错误“无法从对象强制转换为字符”,我想知道是否有人可以向我解释问题是什么,以及我是否可以用另一种不会导致问题的方式来解决一个错误。
This is the bit of code causing the error char op = (char) term;
. It is from an infix to postfix converter
这是导致错误的代码位char op = (char) term;
。它是从中缀到后缀的转换器
//Take terms off the queue in proper order and evaluate
private double evaluatePostfix(DSAQueue<Object> postfixQueue) {
DSAStack<Double> operands = new DSAStack<Double>();
Object term;
//While still terms on queue, take term off.
while(!postfixQueue.isEmpty()) {
term = postfixQueue.dequeue();
if(term instanceof Double) { //If term is double put on stack
operands.push((Double) term);
}
else if(term instanceof Character) { //If term is character, solve
double op1 = operands.pop();
double op2 = operands.pop();
char op = (char) term;
operands.push(executeOperation(op, op2, op1));
}
}
return operands.pop(); //Return evaluated postfix
}
Any help (even pointing me to some reading) would be much appreciated.
任何帮助(甚至指向我一些阅读)将不胜感激。
采纳答案by vikingsteve
You can change this line:
您可以更改此行:
char op = (Character) term;
Explanation: in Java 6you can't cast an Object
to a primitive type, but you cancast it to Character
(which is a class) and unboxing does the rest :)
说明:在 Java 6 中,您不能将 anObject
强制转换为原始类型,但您可以将其Character
强制转换为(这是一个类),然后拆箱完成剩下的工作:)
Edit:Alternatively, you can bump the language level of your project up to Java 7(or 8...)
编辑:或者,您可以将项目的语言级别提高到Java 7(或 8...)
回答by Bohemian
Java can't auto unbox and cast in one operation, which is what you're trying to do. Instead, cast it to Character
and java will auto unbox it OK:
Java 无法在一个操作中自动拆箱和强制转换,而这正是您想要做的。相反,将其Character
强制转换为Java 将自动将其拆箱 OK:
char op = (Character) term;
回答by Maroun
You can't castfrom an objectto a primitivedata type (or vice versa). They are two very different things.
不能投从一个对象到一个原语数据类型(或反之亦然)。它们是两个非常不同的东西。
Java lang
package has classes that correspond to each primitive: Float
for float
, Boolean
for boolean
..
Javalang
包具有对应于每个原语的类:Float
for float
、Boolean
for boolean
..
As others already stated, you should cast the objectto Character
and not char
.
正如其他人已经说过的那样,您应该将对象转换为Character
而不是char
。
回答by MacDaddy
you have to chang
你必须改变
char op = (Character) term;
because you have to cast with class refference not using the primitive type.
因为您必须使用类引用进行转换,而不是使用原始类型。
回答by Syam S
There is nothing wrong with your code. Since term is an instance of Character you could even assign it directly without casting as char op = term if you are using jdk 1.5+
您的代码没有任何问题。由于 term 是 Character 的一个实例,如果您使用的是 jdk 1.5+,您甚至可以直接分配它而不将其转换为 char op = term
One possibility i could see is the compiler compliance level used. You might have a JDK 1.5+ but you are compiling your project in a lower level. If you are using eclipse check for JDK compliance level in Project->properties->Java Compiler
我可以看到的一种可能性是使用的编译器合规性级别。您可能有一个 JDK 1.5+,但您正在以较低的级别编译您的项目。如果您在 Project->properties->Java Compiler 中使用 Eclipse 检查 JDK 合规性级别
If you are directly compiling or using Ant build check for -source & -target options.
如果您直接编译或使用 Ant 构建,请检查 -source 和 -target 选项。
And just for your info, as far as i know Object to primitive casting was introduced in JDK 1.7. So if you are using JDk 1.7+ you could wirte as
仅供参考,据我所知,JDK 1.7 中引入了对象到原始类型转换。因此,如果您使用的是 JDK 1.7+,则可以编写为
Object term = 'C';
char op = (char) term;