与“if”中的 == 和 = 混淆陈述
我知道我们不能像在其他几种语言中那样在 java 的 if 语句中使用赋值运算符。
那
int a;
if(a = 1) { }
会给出编译错误。
但是下面的代码工作正常,怎么样?
boolean b;
if(b = true) { }
编辑:这是规则不能在 if 语句中使用赋值的例外情况吗?
I know that we cant use assignment operator in if statements in java as we use in any other few languages.
that is
int a;
if(a = 1) { }
will give a compilation error.
but the following code works fine, how?
boolean b;
if(b = true) { }
EDIT : Is this the exception to rule that assignment cant be used in if statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为赋值的“结果”是分配的值...所以在第二种情况下它仍然是一个布尔表达式。
if
表达式要求条件为boolean
表达式,第二个表达式满足,但第一个表达式不满足。实际上,您的两个片段是:?
从扩展中可以清楚地看出第二个版本将编译但第一个版本不会编译
这是不直接比较真假的原因之一。所以我总是只写
if (b)
而不是if (b == true)
和if (!b)
而不是if (b == false)
。诚然,当b
和c
是boolean
变量时,您仍然会遇到if (b == c
) 的问题- 那里的拼写错误可能会导致问题。但我不能说这曾经发生在我身上。编辑:响应您的编辑 - 各种赋值都可以在
if
语句中使用 - 和while
循环等,只要整体条件表达式为boolean< /代码>。例如,您可能会:
虽然我通常避免条件中的副作用,但这个特定的习惯用法对于上面显示的示例或使用
InputStream.read
通常很有用。基本上就是“当我读到的值有用时,就使用它”。Because the "result" of an assignment is the value assigned... so it's still a
boolean
expression in the second case.if
expressions require the condition to be aboolean
expression, which is satisfied by the second but not the first. Effectively, your two snippets are:and
Is it clear from that expansion that the second version will compile but not the first?
This is one reason not to do comparisons with true and false directly. So I would always just write
if (b)
instead ofif (b == true)
andif (!b)
instead ofif (b == false)
. You still get into problems withif (b == c
) whenb
andc
areboolean
variables, admittedly - a typo there can cause an issue. I can't say it's ever happened to me though.EDIT: Responding to your edit - assignments of all kinds can be used in
if
statements - andwhile
loops etc, so long as the overall condition expression isboolean
. For example, you might have:While I usually avoid side-effects in conditions, this particular idiom is often useful for the example shown above, or using
InputStream.read
. Basically it's "while the value I read is useful, use it."对于
if
,您需要一个计算结果为布尔值的表达式。b = true
计算结果为 boolean,但a = 1
计算结果为 int,因为赋值始终计算为指定的值。For
if
you need an expression that evaluates to boolean.b = true
evalueates to boolean buta = 1
evaluates to int as assignments always evaluate to the assigned values.第二个代码工作正常的原因是它为“b”分配 true 值,然后比较 b 是 true 还是 false。您可以这样做的原因是因为您可以在 if 语句内执行赋值运算符,并且可以单独与布尔值进行比较。这与执行 if(true) 相同。
The reason the second code works okay is because it is assigning 'b' the value of true, and then comparing to see if b is true or false. The reason you can do this is because you can do assignment operators inside an if statement, AND you can compare against a boolean by itself. It would be the same as doing if(true).
在java中,你没有隐式转换。因此非布尔值或不会自动转换为布尔值。
在第一种情况下,语句的结果是一个 int,它是非布尔值,这是行不通的。最后一种情况,结果是布尔值,可以在 if 语句中计算。
In java, you don't have implicit casting. So non-boolean values or not automatically transformed to booleans.
In the first case, the result of the statements is an int, which is non-boolean, which will not work. The last case, the result is boolean, which can be evaluated in an if-statement.
规则不是“不能在
if
语句中使用赋值”,而是“if
语句中的条件必须是boolean< 类型/代码>”。赋值表达式会生成所分配类型的值,因此如果要分配
boolean
值,Java 仅允许在if
语句中进行赋值。这就是为什么应该避免使用
if (foo == true)
样式,而是简单地编写if (foo)
的原因。The rule is not that "assignment can't be used in an
if
statement", but that "the condition in anif
statement must be of typeboolean
". An assignment expression produces a value of the type being assigned, so Java only permits assignment in anif
statement if you're assigning aboolean
value.This is a good reason why the style
if (foo == true)
should be avoided, and instead simply writeif (foo)
.