这行在 Java 中是什么意思:boolean retry = id == 1;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14686332/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:03:46  来源:igfitidea点击:

What does this line mean in Java: boolean retry = id == 1;

javasyntax

提问by Phat7

I have been learning Java for a while now and still learning new syntax tricks and stuff. I came across this in Android source code:

我已经学习 Java 一段时间了,但仍在学习新的语法技巧和东西。我在 Android 源代码中遇到过这个:

boolean retry = id == 1;

What does it mean?

这是什么意思?

回答by JB Nizet

id == 1is a boolean expression which is true if idequals 1, and false otherwise.

id == 1是一个布尔表达式,如果id等于则为真1,否则为假。

boolean retry = id == 1;declares a boolean variable named retry, and assigns the value of the boolean expression id == 1to this variable.

boolean retry = id == 1;声明一个名为 的布尔变量retry,并将布尔表达式的值赋给id == 1该变量。

So it declares a boolean variable which is true if id == 1, and false otherwise.

所以它声明了一个布尔变量,如果为真id == 1,否则为假。

To make it a bit clearer, you might write it that way:

为了让它更清楚一点,你可以这样写:

boolean retry = (id == 1);

回答by Obl Tobl

retryis trueif idhas the value 1, otherwise retryis false.

retryis trueifid的值为 1,否则retryis false

回答by Peter Lawrey

It is the same as

它与

boolean retry;
if (id == 1)
   retry = true;
else
   retry = false;

回答by Anirudha

==, which is the equality predicate, has a higher precedence than =, which is the assignment operator.

==是相等谓词,其优先级高于=赋值运算符 。

Therefore, id == 1is evaluated first and then its value (either true or false) is assigned to retry.

因此,id == 1首先评估,然后将其值(真或假)分配给retry

回答by duffy356

The boolean retry gets the value of true if id == 1.

如果 ,则布尔重试获得 true 的值id == 1

It's the same as:

它与以下相同:

boolean retry;
if (id == 1) {
    retry = true;
} else {
    retry = false;
}

回答by bas

first the idis evaluated with 1, so presumably idis an integer.

首先 theid用 1 计算,所以大概id是 an integer

After that, the value retryis assigned this evaluation, so if idis equal to 1, retrywill become true, and for any other value of idretrywill become false.

之后,该值retry被赋予这个评估,所以如果id等于 1,retry将变为true,而对于任何其他值,idretry将变为false

回答by ShuklaSannidhya

This line creates a boolean variable and sets it to trueif idis equal to 1 and falseif it is not.

此行创建一个布尔变量并将其设置为trueifid等于 1,false否则设置为不。

回答by Babul Mirdha

It is acts like a ternary operation, (x) ? true : falsein C, C++, C#, etc;

它的作用类似于三元运算,(x) ? true : false在 C、C++、C# 等中;

The similar syntax:

类似的语法:

boolean retry = (id == 1)? true: false; 

Or it can written another way:

或者它可以写成另一种方式:

boolean retry;
if (id == 1) {
    retry = true;
} else {
    retry = false;
}

回答by Zeph

I find that just using parens helps to clear up the confusion behind complex statements like this.

我发现仅使用括号有助于消除此类复杂语句背后的混淆。

boolean retry = (id == 1);Makes much more sense to me. Here it's clear that (id == 1)is an expression being evaluated and the result is being assigned to boolean retry

boolean retry = (id == 1);对我来说更有意义。很明显,这(id == 1)是一个正在评估的表达式,并且结果被分配给boolean retry

回答by PriestVallon

It might be easier to see whats happening if you look at it like this:

如果你这样看,可能更容易看出发生了什么:

boolean retry = (id == 1);

So basically it checks if id equals 1, and then assigns the result to the variable retry.

所以基本上它会检查 id 是否等于 1,然后将结果分配给变量 retry。