'=='和'='之间有什么区别?

时间:2022-04-17 13:53:54

I know that one of them is bitwise and the other is logical but I can not figure this out:

我知道其中一个是按位而另一个是合乎逻辑的,但我无法弄明白:

Scanner sc = new Scanner(System.in);
System.out.println("Enter ur integer");
int x=sc.nextInt();
if(x=0)//Error...it can not be converted from int to boolean
System.out.println("...");

The error means that x cannot be converted to boolean or the result of x=0 can not be converted to boolean.

该错误意味着x无法转换为布尔值,或者x = 0的结果无法转换为布尔值。

12 个解决方案

#1


== checks for equality. = is assignment.

==检查是否平等。 =是作业。

What you're doing is: if( x = Blah ) - in Java this statement is illegal as you can not test the state of an assignment statement. Specifically, Java does not treat assignment as a boolean operation, which is required in an if statement. This is in contrast with C/C++, which DOES allow you to treat assignment as a boolean operation, and can be the result of many hair-pulling bugs.

你正在做的是:if(x = Blah) - 在Java中这个语句是非法的,因为你无法测试赋值语句的状态。具体来说,Java不会将赋值视为布尔运算,这在if语句中是必需的。这与C / C ++形成对比,C / C ++允许您将赋值视为布尔运算,并且可能是许多拔毛错误的结果。

#2


When you write 'x = 0' you are saying "Store 0 in the variable x". The return value on the whole expression is '0' (it's like this so you can say silly things like x = y = 0).

当你写'x = 0'时,你会说“在变量x中存储0”。整个表达式的返回值为'0'(就像这样,你可以说愚蠢的事情,比如x = y = 0)。

When you write 'x == 0' it says "Does x equal 0?". The return value on this expression is going to be either 'true' or 'false'.

当你写'x == 0'时,它会说“x是否等于0?”。此表达式的返回值将为“true”或“false”。

In Java, you can't just say if(0) because if expects a true/false answer. So putting if(x = 0) is not correct, but if(x == 0) is fine.

在Java中,你不能只说if(0)因为if是否需要一个真/假的答案。所以把if(x = 0)不正确,但是如果(x == 0)没问题。

#3


== is a comparison operator, and = is assignment.

==是比较运算符,=是赋值。

#4


== is an equality check. if (x == 0) // if x equals 0
= is an assignment. x = 0; // the value of x is now 0

#5


I know the question has been answered, but this still comes up from time to time not as a programmer error but as a typographical error (i.e., the programmer knew what he meant, but failed). It can be hard to see, since the two look so similar.

我知道问题已得到解答,但这仍然不时出现程序员错误,但作为一个印刷错误(即程序员知道他的意思,但失败了)。很难看出,因为两者看起来很相似。

I've found that a way to help avoid doing this is to put the constant expression on the left-hand-side, like so:

我发现一种帮助避免这样做的方法是将常量表达式放在左侧,如下所示:

if (0 == x) 
   ...

That way, if I accidentally use only one "=" sign, the compiler will fail with an error about assigning to a constant expression, whether or not the assignment operator is left-associative and whether the if conditional expects a strongly-typed Boolean.

这样,如果我不小心只使用了一个“=”符号,编译器将失败,并指出一个常量表达式错误,无论赋值运算符是否为左关联,if条件是否需要强类型布尔值。

#6


if(x=0)

Here you're assigning the value of 0 to the variable x. The if statement in Java can't evaluate an integer argument as it can in many other languages. In Java, if requires a boolean. Try

在这里,您将值0赋给变量x。 Java中的if语句无法像许多其他语言一样评估整数参数。在Java中,如果需要布尔值。尝试

if(x == 0)

to do a comparison.

做比较。

#7


Interpret the error to mean

将错误解释为含义

"The expression

x=0

cannot be converted to Boolean."

无法转换为布尔值。“

#8


Just to clarify about C/C++ - assignment is evaluated to the right operand

只是为了澄清C / C ++ - 赋值被评估为正确的操作数

if(a = n)

is evaluated to n, so (n = 1) is true (n = 0) is false

被评估为n,因此(n = 1)为真(n = 0)为假

#9


One interesting note: Since assignment operator evaluates to the right operand, the following is valid in Java(albeit not pretty):

一个有趣的注意事项:由于赋值运算符求值为右操作数,因此以下在Java中有效(尽管不是很漂亮):

if (( x = blah ) > 0) ...

Parenthesis are needed because of operator precedence ( '>' binds stronger than '=').

由于运算符优先级('>'绑定强于'=',因此需要括号)。

#10


As others have already said, '=' is assignment; '==' is compare.

正如其他人已经说过的那样,'='就是任务; '=='是比较。

in your program change

在你的程序更改

if(x=0)

to

if(x==0)

#11


"==" checks for equality



"=" Is used for assignment. 

It is giving you error cause you're assigning value to x in if(), where you're supposed to check for the equality. Try changing it to equality instead of assignment operator.

它给你错误,因为你在if()中为x分配值,你应该检查是否相等。尝试将其更改为相等而不是赋值运算符。

#12


As others stated, = assigns while == compares.

正如其他人所述,= = = = =。

However, these statements have their own values as well.

但是,这些陈述也有自己的价值观。

The = operator returns the value of its right-hand operand. This is how statements like a = b = c = 5 work: they are parsed as a = (b = (c = 5)), which evaluates to a = (b = 5) and then a = 5.

=运算符返回其右侧操作数的值。这就是a = b = c = 5这样的语句如何工作:它们被解析为a =(b =(c = 5)),其计算结果为a =(b = 5)然后a = 5。

The == operator returns a boolean that is true if its operands are equal. The if statement runs its body if its argument is true. Thus, if headers like if (5 == 5) translate to if (true). This is why sometimes you see infinite while loops with header while (true); the while loop runs "while" toe argument is true.

==运算符返回一个布尔值,如果其操作数相等则为true。 if语句如果其参数为true则运行其主体。因此,如果if(5 == 5)之类的标题转换为if(true)。这就是为什么有时你会看到带有标题的无限while循环(true); while循环运行“while”toe参数为true。

If you had a boolean in your if statement, it would give no error and run the code if the value being assigned (or "compared to") was true. This is why it is so important to never mix up the = and == operators, especially when working with booleans.

如果你的if语句中有一个布尔值,那么如果赋值(或“比较”)为真,则它不会给出错误并运行代码。这就是为什么永远不要混淆=和==运算符,特别是在使用布尔值时非常重要的原因。

Hope this helped!!

希望这有帮助!!

#1


== checks for equality. = is assignment.

==检查是否平等。 =是作业。

What you're doing is: if( x = Blah ) - in Java this statement is illegal as you can not test the state of an assignment statement. Specifically, Java does not treat assignment as a boolean operation, which is required in an if statement. This is in contrast with C/C++, which DOES allow you to treat assignment as a boolean operation, and can be the result of many hair-pulling bugs.

你正在做的是:if(x = Blah) - 在Java中这个语句是非法的,因为你无法测试赋值语句的状态。具体来说,Java不会将赋值视为布尔运算,这在if语句中是必需的。这与C / C ++形成对比,C / C ++允许您将赋值视为布尔运算,并且可能是许多拔毛错误的结果。

#2


When you write 'x = 0' you are saying "Store 0 in the variable x". The return value on the whole expression is '0' (it's like this so you can say silly things like x = y = 0).

当你写'x = 0'时,你会说“在变量x中存储0”。整个表达式的返回值为'0'(就像这样,你可以说愚蠢的事情,比如x = y = 0)。

When you write 'x == 0' it says "Does x equal 0?". The return value on this expression is going to be either 'true' or 'false'.

当你写'x == 0'时,它会说“x是否等于0?”。此表达式的返回值将为“true”或“false”。

In Java, you can't just say if(0) because if expects a true/false answer. So putting if(x = 0) is not correct, but if(x == 0) is fine.

在Java中,你不能只说if(0)因为if是否需要一个真/假的答案。所以把if(x = 0)不正确,但是如果(x == 0)没问题。

#3


== is a comparison operator, and = is assignment.

==是比较运算符,=是赋值。

#4


== is an equality check. if (x == 0) // if x equals 0
= is an assignment. x = 0; // the value of x is now 0

#5


I know the question has been answered, but this still comes up from time to time not as a programmer error but as a typographical error (i.e., the programmer knew what he meant, but failed). It can be hard to see, since the two look so similar.

我知道问题已得到解答,但这仍然不时出现程序员错误,但作为一个印刷错误(即程序员知道他的意思,但失败了)。很难看出,因为两者看起来很相似。

I've found that a way to help avoid doing this is to put the constant expression on the left-hand-side, like so:

我发现一种帮助避免这样做的方法是将常量表达式放在左侧,如下所示:

if (0 == x) 
   ...

That way, if I accidentally use only one "=" sign, the compiler will fail with an error about assigning to a constant expression, whether or not the assignment operator is left-associative and whether the if conditional expects a strongly-typed Boolean.

这样,如果我不小心只使用了一个“=”符号,编译器将失败,并指出一个常量表达式错误,无论赋值运算符是否为左关联,if条件是否需要强类型布尔值。

#6


if(x=0)

Here you're assigning the value of 0 to the variable x. The if statement in Java can't evaluate an integer argument as it can in many other languages. In Java, if requires a boolean. Try

在这里,您将值0赋给变量x。 Java中的if语句无法像许多其他语言一样评估整数参数。在Java中,如果需要布尔值。尝试

if(x == 0)

to do a comparison.

做比较。

#7


Interpret the error to mean

将错误解释为含义

"The expression

x=0

cannot be converted to Boolean."

无法转换为布尔值。“

#8


Just to clarify about C/C++ - assignment is evaluated to the right operand

只是为了澄清C / C ++ - 赋值被评估为正确的操作数

if(a = n)

is evaluated to n, so (n = 1) is true (n = 0) is false

被评估为n,因此(n = 1)为真(n = 0)为假

#9


One interesting note: Since assignment operator evaluates to the right operand, the following is valid in Java(albeit not pretty):

一个有趣的注意事项:由于赋值运算符求值为右操作数,因此以下在Java中有效(尽管不是很漂亮):

if (( x = blah ) > 0) ...

Parenthesis are needed because of operator precedence ( '>' binds stronger than '=').

由于运算符优先级('>'绑定强于'=',因此需要括号)。

#10


As others have already said, '=' is assignment; '==' is compare.

正如其他人已经说过的那样,'='就是任务; '=='是比较。

in your program change

在你的程序更改

if(x=0)

to

if(x==0)

#11


"==" checks for equality



"=" Is used for assignment. 

It is giving you error cause you're assigning value to x in if(), where you're supposed to check for the equality. Try changing it to equality instead of assignment operator.

它给你错误,因为你在if()中为x分配值,你应该检查是否相等。尝试将其更改为相等而不是赋值运算符。

#12


As others stated, = assigns while == compares.

正如其他人所述,= = = = =。

However, these statements have their own values as well.

但是,这些陈述也有自己的价值观。

The = operator returns the value of its right-hand operand. This is how statements like a = b = c = 5 work: they are parsed as a = (b = (c = 5)), which evaluates to a = (b = 5) and then a = 5.

=运算符返回其右侧操作数的值。这就是a = b = c = 5这样的语句如何工作:它们被解析为a =(b =(c = 5)),其计算结果为a =(b = 5)然后a = 5。

The == operator returns a boolean that is true if its operands are equal. The if statement runs its body if its argument is true. Thus, if headers like if (5 == 5) translate to if (true). This is why sometimes you see infinite while loops with header while (true); the while loop runs "while" toe argument is true.

==运算符返回一个布尔值,如果其操作数相等则为true。 if语句如果其参数为true则运行其主体。因此,如果if(5 == 5)之类的标题转换为if(true)。这就是为什么有时你会看到带有标题的无限while循环(true); while循环运行“while”toe参数为true。

If you had a boolean in your if statement, it would give no error and run the code if the value being assigned (or "compared to") was true. This is why it is so important to never mix up the = and == operators, especially when working with booleans.

如果你的if语句中有一个布尔值,那么如果赋值(或“比较”)为真,则它不会给出错误并运行代码。这就是为什么永远不要混淆=和==运算符,特别是在使用布尔值时非常重要的原因。

Hope this helped!!

希望这有帮助!!