Return value of assignment operation in Java

sunder picture sunder · Jul 2, 2016 · Viewed 15.9k times · Source

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?

Answer

Mureinik picture Mureinik · Jul 2, 2016

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