与“if”中的 == 和 = 混淆陈述

发布于 2024-08-24 02:51:43 字数 290 浏览 2 评论 0原文

我知道我们不能像在其他几种语言中那样在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

破晓 2024-08-31 02:51:43

因为赋值的“结果”是分配的值...所以在第二种情况下它仍然是一个布尔表达式。 if 表达式要求条件为 boolean 表达式,第二个表达式满足,但第一个表达式不满足。实际上,您的两个片段是:

int a;

a = 1;
if (a) { }

boolean b;

b = true;
if (b) { }

从扩展中可以清楚地看出第二个版本将编译但第一个版本不会编译

这是不直接比较真假的原因之一。所以我总是只写 if (b) 而不是 if (b == true)if (!b) 而不是 if (b == false)。诚然,当 bcboolean 变量时,您仍然会遇到 if (b == c) 的问题- 那里的拼写错误可能会导致问题。但我不能说这曾经发生在我身上。

编辑:响应您的编辑 - 各种赋值都可以在 if 语句中使用 - 和 while 循环等,只要整体条件表达式为 boolean< /代码>。例如,您可能会:

String line;
while ((line = reader.readLine()) != null)
{
    // Do something with a line
}

虽然我通常避免条件中的副作用,但这个特定的习惯用法对于上面显示的示例或使用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 a boolean expression, which is satisfied by the second but not the first. Effectively, your two snippets are:

int a;

a = 1;
if (a) { }

and

boolean b;

b = true;
if (b) { }

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 of if (b == true) and if (!b) instead of if (b == false). You still get into problems with if (b == c) when b and c are boolean 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 - and while loops etc, so long as the overall condition expression is boolean. For example, you might have:

String line;
while ((line = reader.readLine()) != null)
{
    // Do something with a line
}

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."

绝不服输 2024-08-31 02:51:43

对于if,您需要一个计算结果为布尔值的表达式。 b = true 计算结果为 boolean,但 a = 1 计算结果为 int,因为赋值始终计算为指定的值。

For if you need an expression that evaluates to boolean. b = true evalueates to boolean but a = 1 evaluates to int as assignments always evaluate to the assigned values.

童话 2024-08-31 02:51:43

第二个代码工作正常的原因是它为“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).

迷离° 2024-08-31 02:51:43

在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.

a√萤火虫的光℡ 2024-08-31 02:51:43

规则不是“不能在 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 an if statement must be of type boolean". An assignment expression produces a value of the type being assigned, so Java only permits assignment in an if statement if you're assigning a boolean value.

This is a good reason why the style if (foo == true) should be avoided, and instead simply write if (foo).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文