sunder sunder - 5 months ago 10
Java Question

Return value of assignment operation in java

I encountered a statement in java

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


How assignment operations return value in Java? As the statement we are checking is
line=reader.readLine()
and comparing it with
null
. Because
readLine
will return string , how exactly we are checking for
null
?

Answer

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.

Comments