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
?
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'.
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.
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.
The simple assignment operator ( = ) is used to assign a value to a variable. The assignment operation evaluates to the assigned value.
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.
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