while loop with a readLine()

13,803

Solution 1

while((a = b.readLine()) != null){ ... }

At each iteration, the condition inside the parentheses is evaluated.

Evaluating this expression consists in calling b.readLine(), affecting the returned value to a, and comparing a with null.

Calling readLine(), as documented, consists in blocking until the next line is read. If there is no next line, readLine() returns null.

So, in short, this while loop reads every line from the reader, does something with the line (inside the while block) and stops when the end of the stream is reached.

Solution 2

The following code snippet

String a;
while((a = b.readLine()) != null) { // a = b.readLine() -> a -> a != null
    ... 
}

is equivalent to

String a = b.readLine();
while(a != null) {
    ...
    a = b.readLine();
}

but the first way is more simple, readable, and Java allows using assignment operators here because they return the value of a renewed variable.

Solution 3

This idiom

while ( (a = b.readLine()) != null) {  /* body */ }

is a normal while loop, but the condition happens to contain an embedded assignment operator, which yields the result of the assignment, which is compared to null.

Whether b is reading from a socket, a file, or any other input stream, presumably b is something like a BufferedReader with its readLine() method. This method will only return null when the end of the stream is reached. It will block if the end of the stream hasn't been reached yet, but not further input consisting of a newline character has been read off the stream yet.

When such a line is available, a is not null and the body of the loop executes. The result of readLine is assigned to a for convenient processing withing the body of the loop.

When the end of the stream is reached, a is null and the while loop ends.

This idiom allows for easy processing of an entire stream of data, whether it is from an entire file, from an entire socket connection, or otherwise generally reading from an entire input stream of data. It looks more complicated than a simpler while loop, but it's just a standard while loop with a more complicated condition.

Share:
13,803
Seba
Author by

Seba

Updated on June 06, 2022

Comments

  • Seba
    Seba almost 2 years

    Here is my question: normal while loop looks like this -

    while(condition){statements} 
    

    and statements execute until condition becomes false and the loop is over. I'm interested in logic of this kind of statement:

    while((a=b.readLine) != null) { ... } 
    

    It's used in client - server communication in my case. The condition is sometimes true, sometimes isn't, but loop looks like it's testing the condition forever and when true, statements in {} execute. It looks to me that loop waits for the condition to be true and then runs statements. Is it somehow linked to the way BufferedReader and InputStreamReader work or what? Also, it seems this loop is never over, it just waits for a condition to be true and then runs statements, and then again waits for a condition to be true, etc. I would be thankful for any clarification.