简体   繁体   English

这行在Java中意味着什么:boolean retry = id == 1;

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

I have been learning Java for a while now and still learning new syntax tricks and stuff. 我已经学习Java一段时间了,还在学习新的语法技巧和东西。 I came across this in Android source code: 我在Android源代码中遇到过这个:

boolean retry = id == 1;

What does it mean? 这是什么意思?

id == 1 is a boolean expression which is true if id equals 1 , and false otherwise. id == 1是一个布尔表达式,如果id等于1则为true,否则为false。

boolean retry = id == 1; declares a boolean variable named retry , and assigns the value of the boolean expression id == 1 to this variable. 声明一个名为retry的布尔变量,并将布尔表达式id == 1的值赋给此变量。

So it declares a boolean variable which is true if id == 1 , and false otherwise. 所以它声明了一个布尔变量,如果id == 1则为true,否则为false。

To make it a bit clearer, you might write it that way: 为了使它更清晰,你可以这样写:

boolean retry = (id == 1);

retrytrue ,如果id的值为1,否则retryfalse

It is the same as 它是一样的

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

== , 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 ,然后将其值(true或false)分配给retry

The boolean retry gets the value of true if id == 1 . 如果id == 1 ,则布尔重试的值为true。

It's the same as: 它与以下相同:

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

first the id is evaluated with 1, so presumably id is an integer . 首先用1来计算id ,所以推测id是一个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

此行创建一个布尔变量,如果id等于1则将其设置为true否则设置为false

It is acts like a ternary operation, (x) ? true : false 它像三元运算一样, (x) ? true : false (x) ? true : false in C, C++, C#, etc; (x) ? true : false C,C ++,C#等中的(x) ? true : false ;

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;
}

I find that just using parens helps to clear up the confusion behind complex statements like this. 我发现只使用parens有助于消除这种复杂语句背后的混乱。

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 这里很明显(id == 1)是一个被评估的表达式,结果被分配给boolean retry

It is a way of defining a boolean variable. 这是一种定义布尔变量的方法。

When id is 1 , the value of retry will be true . id1retry值将为true

retry assigns an expression which will be either true or false as retry is a boolean . retry指定一个表达式,该表达式将为true或false,因为retry是一个boolean Further, == will be solved first and then it will be assigned to retry . 此外, ==将首先解决,然后将分配retry

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。

It is basically the same as retry = (id == 1) . 它与retry = (id == 1)基本相同。 It is evaluating the boolean expression, and assigning it to retry . 它正在评估布尔表达式,并将其指定为retry

The Boolean variable retry will get value 0 or 1 depending on whether the expression id==1 returns true or false . 布尔变量retry将获得值01具体取决于表达式id==1返回true还是false

If value of id is 1 , then id==1 will correspond to true , thus retry=1 . 如果id值为1 ,则id==1将对应于true ,因此retry=1

And if value of id is 0 , then id==1 will correspond to false , thus retry=0 . 如果id值为0 ,则id==1将对应于false ,因此retry=0

Please note that == is a comparison operator. 请注意, ==是一个比较运算符。

The code can write just like this,then it will be understood easily,do you think so? 代码可以像这样编写,然后很容易理解,你这么认为吗? Last, thanks you for giving the chance to answer the question! 最后,感谢您给我机会回答这个问题!

boolean retry = (id == 1);

I will base my response on the assumption that id is an int hence the comparison against 1 is proper and a compilation error is not in place. 我将基于id为int的假设进行响应,因此与1的比较是正确的,并且编译错误不到位。 == is the equality operator in java as described in section 15.21.1 of the JLS. ==是java中的等于运算符,如JLS的第15.21.1节所述。 Being a boolean operator, == will output a boolean value. 作为布尔运算符,==将输出一个布尔值。 = is the java's assignment operator, in this particular case it's the compound assignment operator having the following syntax: =是java的赋值运算符,在这种特殊情况下,它是具有以下语法的复合赋值运算符

boolean f = (op1 op op2) 布尔值f =(op1 op op2)

In translation = assigns the output value of the (op1 op op2) operation to the left operand, in this case f . 在translation =中,将(op1 op op2)操作的输出值分配给左操作数,在本例中为f

Looking back to your sample, the output of id == 1 (true if id has the value 1, false otherwise) is assigned to retry . 回顾一下您的示例, id == 1的输出(如果id的值为1则为true,否则为false)将被指定为重试

To conclude in plain english, your sample has the following meaning: Retry as long as id has the value 1 . 用简单的英语结束,您的示例具有以下含义: 只要id的值为1,就重试

1.int id = 1;
  boolean retry = id == 1;

which means retry = true; 这意味着retry = true; .

2.int id = 2;
  boolean retry = id == 1;

which means retry = false; 这意味着retry = false; .

Simplification id == 1 can be consider as 简化id == 1可以视为

if ( id == 1 ){
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM