Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
up vote 6 down vote accepted

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.

share|improve this answer

Assignment expressions are evaluated to their assignment value.

(test = read.readLine())

>>

(test = <<return value>>)

>>

<<return value>>
share|improve this answer
    
my question is do expressions also return something as according to you at that time (line=null), will that mean it will again return null back? – sunder Jul 2 '16 at 19:57
    
@sunder I edited my answer to address your question about the evaluation of assignment expressions. – Drew Beres Jul 2 '16 at 20:16

(line = reader.readLine()) != null

means

  1. the method readLine() is invoked.
  2. the result is assigned to variable line,
  3. the new value of line will be proof against null

maybe many operations at once...

share|improve this answer

reader.readLine() reads and returns a line for you. Here, you assigned whatever returned from that to line and check if the line variable is null or not.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.