java 什么导致“不兼容的操作数类型”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7215875/
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
What causes the "Incompatible operand types" error?
提问by alan
I am trying to implement iSortableStack Interface via a class.
我正在尝试通过类实现 iSortableStack 接口。
Here's my main function,
这是我的主要功能,
public class SampleStack<E> {
E ch;
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
ISortableStack<Character> s = new SortableStack<Character>();
SampleStack demo = new SampleStack();
while ((demo.ch == System.in.read()) != '\n')
if (!s.isFull())
s.push((Character) demo.ch);
while (!s.isEmpty())
System.out.print(s.pop());
System.out.println();
}
}
But I am getting one error, on this line,
但我在这一行遇到一个错误,
while ((demo.ch == System.in.read()) != '\n')
Error : Incompatible operand types Object and int
错误:不兼容的操作数类型 Object 和 int
What is wrong here ?
这里有什么问题?
回答by erickson
There are two severe problems here that have nothing to do with generics.
这里有两个严重的问题与泛型无关。
First, demo.ch == System.in.read()
is a boolean
expression. The result of read()
(an int
) will be auto-boxed to an Integer
, and the identity of that object will be tested against demo.ch
(which is null
).
首先,demo.ch == System.in.read()
是一个boolean
表达。read()
(an int
)的结果将被自动装箱为 an Integer
,并且该对象的身份将针对demo.ch
(即null
)进行测试。
I think that what you want here is the assignment operator, =
. This will assign the read()
result to demo.ch
.
我认为您在这里想要的是赋值运算符,=
. 这会将read()
结果分配给demo.ch
.
The next problem is that it looks like you expect demo.ch
to be a Character
(based on the casts you are using). However, you are trying to assign an int
(the result of read()
) to it. Primitive types can be "auto-boxed" when necessary, that is, they can be converted to a wrapper object like Character
or Integer
, but only when the value to be converted is a constant expression that can be represented by the target type. Here, the value is variable, so the conversion cannot be performed implicitly.
下一个问题是看起来您希望demo.ch
成为一个Character
(基于您正在使用的演员表)。但是,您正在尝试为其分配一个int
(的结果read()
)。原始类型在必要时可以“自动装箱”,即可以将它们转换为类似Character
or的包装对象Integer
,但前提是要转换的值是可以由目标类型表示的常量表达式。此处,该值是可变的,因此无法隐式执行转换。
You could work around this by explicitly casting the read()
result to a char
, and then letting the auto-boxing convert it to a Character
, but that would hide EOF, which is represented by a value of -1. I recommend using something like this instead:
您可以通过将read()
结果显式转换为 a char
,然后让自动装箱将其转换为 a 来解决此问题Character
,但这会隐藏由值 -1 表示的 EOF。我建议使用这样的东西:
while (true) {
int ch = System.in.read();
if ((ch < 0) || (ch == '\n'))
break;
if (!s.isFull())
s.push((char) ch);
}
Note that we don't use demo
here at all, so the problems with its type parameter are irrelevant.
注意我们demo
这里根本没有使用,所以它的类型参数的问题是无关紧要的。
回答by Jeffrey
SampleStack.ch
is of type E
. E
is an object specified by your type parameters. Since you did not specify a type parameter, the compiler puts Object
in for you. If you wanted ch
to be a Character
, you would want SampleStack<Character> demo = new SampleStack<Character>();
or in Java 7 SampleStack<Character> demo = new SampleStack<>();
.
SampleStack.ch
是 类型E
。E
是由您的类型参数指定的对象。由于您没有指定类型参数,编译器会Object
为您输入。如果你想ch
成为一个Character
,你会想要SampleStack<Character> demo = new SampleStack<Character>();
或 在 Java 7 中SampleStack<Character> demo = new SampleStack<>();
。
回答by George Armhold
You have ==
(equality test) when you want =
(assignment). You're never actually assigning to demo.ch
. The equality test returns boolean, rather than char, hence the error message.
==
当你想要=
(分配)时,你有(平等测试)。你从来没有真正分配给demo.ch
. 相等测试返回布尔值,而不是字符,因此是错误消息。
You will also need to cast the result from System.in.read()
to a character from an integer (or else use SampleStack<Integer>
, or something like that.)
您还需要将结果从System.in.read()
整数转换为字符(或者使用SampleStack<Integer>
,或类似的东西。)
回答by Ernest Friedman-Hill
You haven't provided a type parameter when you instantiate SampleStack
, so demo.ch
is of type Object
. That obviously can't be compared (or assigned, which is what I suspect you actually wanted to do, anyway) from the int
coming from System.in
.
您在实例化时没有提供类型参数SampleStack
,所以demo.ch
是 type Object
。这显然无法与int
来自System.in
.
回答by Mateusz Dymczyk
You have several errors in this code:
您在此代码中有几个错误:
as people pointed out you're making a generic class but you're not generalizing it and using it raw, you need:
SampleStack<Character>
even if you change it it wont run as you have == instead of =
even if you change the above two it wont work as System.in.read() returns an int, not a character, You'd need to either make a stack of Integers OR read the value from the input to a variable and then cast it but its not a good practice. I'd use a Scanner or somethign similar to read what the user inputs like this:
Scanner sc = new Scanner(System.in); char c = sc.nextChar();
正如人们指出的那样,您正在创建一个通用类,但您并没有对其进行概括和使用原始类,您需要:
SampleStack<Character>
即使您更改它,它也不会像您一样运行 == 而不是 =
即使您更改上述两个它也不会工作,因为 System.in.read() 返回一个 int,而不是一个字符,您需要要么制作一堆整数,要么将输入中的值读取到一个变量中,然后进行转换它,但它不是一个好习惯。我会使用扫描仪或类似的东西来读取用户输入的内容,如下所示:
Scanner sc = new Scanner(System.in); char c = sc.nextChar();