What does this line mean in Java: boolean retry = id == 1;
JavaSyntaxJava Problem Overview
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:
boolean retry = id == 1;
What does it mean?
Java Solutions
Solution 1 - Java
id == 1
is a boolean expression which is true if id
equals 1
, and false otherwise.
boolean retry = id == 1;
declares a boolean variable named retry
, and assigns the value of the boolean expression id == 1
to this variable.
So it declares a boolean variable which is true if id == 1
, and false otherwise.
To make it a bit clearer, you might write it that way:
boolean retry = (id == 1);
Solution 2 - Java
retry
is true
if id
has the value 1, otherwise retry
is false
.
Solution 3 - Java
It is the same as
boolean retry;
if (id == 1)
retry = true;
else
retry = false;
Solution 4 - Java
==
, 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
.
Solution 5 - Java
The boolean retry gets the value of true if id == 1
.
It's the same as:
boolean retry;
if (id == 1) {
retry = true;
} else {
retry = false;
}
Solution 6 - Java
first the id
is evaluated with 1, so presumably id
is 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
.
Solution 7 - Java
This line creates a boolean variable and sets it to true
if id
is equal to 1 and false
if it is not.
Solution 8 - Java
It is acts like a ternary operation, (x) ? true : false
in C, C++, C#, etc;
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;
}
Solution 9 - Java
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
Solution 10 - Java
It is a way of defining a boolean variable.
When id
is 1
, the value of retry
will be true
.
Solution 11 - Java
retry
assigns an expression which will be either true or false as retry
is a boolean
.
Further, ==
will be solved first and then it will be assigned to retry
.
Solution 12 - Java
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.
Solution 13 - Java
It is basically the same as retry = (id == 1)
. It is evaluating the boolean expression, and assigning it to retry
.
Solution 14 - Java
The Boolean variable retry
will get value 0
or 1
depending on whether the expression id==1
returns true
or false
.
If value of id
is 1
, then id==1
will correspond to true
, thus retry=1
.
And if value of id
is 0
, then id==1
will correspond to false
, thus retry=0
.
Please note that ==
is a comparison operator.
Solution 15 - Java
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. == is the equality operator in java as described in section 15.21.1 of the JLS. 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: > boolean f = (op1 op op2)
In translation = assigns the output value of the (op1 op op2)
operation to the left operand, in this case f.
Looking back to your sample, the output of id == 1
(true if id has the value 1, false otherwise) is assigned to retry.
To conclude in plain english, your sample has the following meaning: Retry as long as id has the value 1.
Solution 16 - Java
1.int id = 1;
boolean retry = id == 1;
which means retry = true;
.
2.int id = 2;
boolean retry = id == 1;
which means retry = false;
.
Simplification id == 1 can be consider as
if ( id == 1 ){
}
Solution 17 - Java
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);