0

I have this line of code from a tutorial

while((line = bufferedReader.readLine()) != null)

It worked fine but i don't get the concept. Does Java asign a value to the operation itself line = bufferedReader.readLine()? After that looks like:

line and line = bufferedReader.readLine() has the same value?.

Trying to get it i tested

  String a = "Not null";
  String b;
  System.out.println( (b = a) );

The output was: Not null.

The question is. The operaton of assigning itself has (At least for an instant) the same value of whatever is at the right of "="?.

2
  • 2
    line = bufferedReader.readLine is not a value, it's an assignment. What happens is that the readLine method is called, the return value is assigned to line, and after that, the result (the line) is checked to be not null. (And a bit offtopic: I don't think this question deserves the downvotes, it's not a crazy question to ask for some explanation) Commented Jul 20, 2014 at 18:51
  • 1
    “This may be a some sort of stupid question that will receive negative votes” ask your question already! We will tell you if it's stupid. And please do not use “Thank you very much.” either (see stackoverflow.com/help/behavior ) Commented Jul 20, 2014 at 18:53

5 Answers 5

3

It's an example of assignment conversion. The result of an assignment is the value of the assignment.

int a;
int b = a = 0;

From jls-5.2,

Assignment conversion occurs when the value of an expression is assigned (§15.26) to a variable: the type of the expression must be converted to the type of the variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I did not visualize as assignment chaining
I am sorry fot having forgotten to accept the answer.
2

In your example of:

while((line = bufferedReader.readLine()) != null)

line is assigned to bufferedReader.readLine() and then checked to see if it is null.

In your second example of:

String a = "Not null";
String b;
System.out.println((b = a));

b is assigned to a (which is "Not null") and then printed, that's why you see "Not null" as the output.

Comments

1

The assignment operator (=) returns the value that was assigned. The line from your tutorial first evaluates

bufferedReader.readLine()

The result is null if there are no more lines left to be read. null != null evaluates to false, causing the while loop to exit when there is no more input.

Similarly

b = a

assigns the value "Not Null" to b, and returns "Not Null"

Comments

1

It produces the same output as:

line = bufferedReader.readLine();
while(line != null) {
    //do stuff in the loop
    line = bufferedReader.readLine();
}

Before the test in the while loop, a line is read from the buffered reader and assigned to the variable line. The value of line is then checked against null.
This is a form of syntactic sugar, which is used to make the code shorter or easier to read.

Comments

0

In java Always assign from right to left and return left most variable. Here is an example

public class Test {
static String a="this is a";
static String b= "this is b";
static String c="this is c";
public static void main(String[] args) {


    //Simple assignments
   //Output=this is c
    System.out.println(a=b=c);//assign c value to b and then assign b value to a and return a

}   

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.