这行在 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
What does this line mean in Java: boolean retry = id == 1;
提问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 == 1
is a boolean expression which is true if id
equals 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 == 1
to 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
retry
is true
if id
has the value 1, otherwise retry
is false
.
retry
is true
ifid
的值为 1,否则retry
is 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 == 1
is 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 id
is evaluated with 1, so presumably id
is an integer
.
首先 theid
用 1 计算,所以大概id
是 an integer
。
After that, the value retry
is assigned this evaluation, so if id
is equal to 1, retry
will become true
, and for any other value of id
retry
will become false
.
之后,该值retry
被赋予这个评估,所以如果id
等于 1,retry
将变为true
,而对于任何其他值,id
retry
将变为false
。
回答by ShuklaSannidhya
This line creates a boolean variable and sets it to true
if id
is equal to 1 and false
if it is not.
此行创建一个布尔变量并将其设置为true
ifid
等于 1,false
否则设置为不。
回答by Babul Mirdha
It is acts like a ternary operation, (x) ? true : false
in 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。