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?
== is an operator that returns true if the contents being compared refer to the same memory or false if they don't. If two strings compared with == refer to the same string memory, the return value is true; if not, it is false. The return value of == above is false, as "MYTEXT" and "YOURTEXT" refer to different memory.
In Java, the boolean keyword is a primitive data type. It is used to store only two possible values, either true or false. It specifies 1-bit of information and its "size" can't be defined precisely. The boolean keyword is used with variables and methods. Its default value is false.
An if statement checks a boolean value and only executes a block of code if that value is true . To write an if statement, write the keyword if , then inside parentheses () insert a boolean value, and then in curly brackets {} write the code that should only execute when that value is true .
if (false) means peace of code which is never executed. If some of your code is unused you should remove it.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With