Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an assignment expression evaluate to in Java?

I encountered a statement in Java

while ((line = reader.readLine()) != null) {     out.append(line); } 

How do assignment operations return a value in Java?

The statement we are checking is line = reader.readLine() and we compare it with null.

Since readLine will return a string, how exactly are we checking for null?

like image 480
sunder Avatar asked Jul 02 '16 19:07

sunder


People also ask

What is assignment expression in Java?

The assignment operator denoted by the single equal sign =. In a Java assignment statement, any expression can be on the right side and the left side must be a variable name. For example, this does not mean that "a" is equal to "b", instead, it means assigning the value of 'b' to 'a'.

What is the value of an assignment expression?

The value of an assignment expression is the value of the right-side operand. As a side effect, the = operator assigns the value on the right to the variable or property on the left so that future references to the variable or property evaluate to the value.

What is an assignment expression?

Assignment expressions allow variable assignments to occur inside of larger expressions. While assignment expressions are never strictly necessary to write correct Python code, they can help make existing Python code more concise.

What assignment operator assigns a value to a variable?

The simple assignment operator ( = ) is used to assign a value to a variable. The assignment operation evaluates to the assigned value.


1 Answers

The assignment operator in Java evaluates to the assigned value (like it does in, e.g., c). So here, readLine() will be executed, and its return value stored in line. That stored value is then checked against null, and if it's null then the loop will terminate.

like image 94
Mureinik Avatar answered Sep 30 '22 22:09

Mureinik