Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

syntax

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?

like image 354
RufusInZen Avatar asked Feb 04 '13 12:02

RufusInZen


People also ask

Why == is false in Java?

== 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.

What does boolean false mean in Java?

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.

How do you do a boolean in an if statement?

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 .

What does if false mean Java?

if (false) means peace of code which is never executed. If some of your code is unused you should remove it.


1 Answers

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); 
like image 145
JB Nizet Avatar answered Sep 24 '22 19:09

JB Nizet