Why does java print the value of the variable that is assigned it in the code below? I want understand what is happening here.
My thought is that the expression inside println() method is evaluated first. For the first example, 5 is assigned to int x. At that point, does x become available for the println() method to print its value?
This code confuses me. I wouldn’t have thought expression like x=5 could return a value. Thank you.
Examples:
int x;
System.out.println(x=5); //prints 5
boolean isValid;
System.out.println(isValid = true); //prints true
String fruit;
System.out.println(fruit="Orange"); //prints orange
7
The parameter of of a method can be an expression.
System.out.println(x - y)
This works because x - y
is an expression.
In most C like languages, the assignment operator is also part of an expression. For example in C# Why do assignment statements return a value? Eric Lippert points out the difference between a statement an an expression. An expression returns something. +
, -
, !
and so on – those operators return things. The =
operator is no different in this respect, it returns something too.
That an assignment operator returns a value is idiomatic in C like languages. And while the original thoughts of why that was the case in C are lost, x = y
is just as much of an expression as x - y
is.
It turns out that x = 42
returns the value of 42
also. This is useful for side effects like x = y = z = 42
which is actually x = (y = (z = 42)))
. And here, z = 42
returns 42
, which is then used in y = 42
and so forth.
You will also see the return value used in things such as:
while ((line = in.readLine()) != null) {
processLine(++lineNo, line);
}
where the assignment to line is done and checked to see if it is not null.
This is perfectly idiomatic code for most languages that trace a linage back to C.
Further reading: Java Language Specification, section 15.26: Assignment Operators
1
When making an assignment, the value assigned is considered the “result” or “return value” of the expression. It’s subtle and maybe not clear because your example is something that is rarely done.
For example:
int i = 5;
int j = (i = 6);
At this point in the code, both i
and j
are equal to 6. We assigned i to the value 6 and then captured the return value of the expression, which is the value which was transferred, which is 6.
Personally, I would avoid this because it can be very confusing and easy to introduce bugs. Especially in a System.out.println or any call that is expected to not change the state. It violates the principle of least astonishment.
Ever wonder why programming classes teach you to always use a double-equals (==
) in an if statement? Ever noticed that with a single equal sign, it may still work? This is why. The process of evaluating a condition is changing the condition, creating a truism or falsism. The number of software defects caused by if (x = y)
…..
Many programmers (I have not picked up the habit yet, but probably ought to) prefer to write if statements with the constant on the left side like if (5 == x)
instead of if (x == 5)
so that if they accidentally use a single equal sign, the compiler won’t allow it.
To take this to your code example, you are right, the expression is “returning” a value.
String fruit;
System.out.println(fruit="Orange"); //prints orange
When the JVM runs this code, one instruction at a time, it sees 3 instructions in this order:
String fruit;
String someInternalStackVariableYouDontSee = (fruit = "Orange");
System.out.println(someInternalStackVariableYouDontSee );
1
Java is fairly liberal about where you can do an assignment. It is printing the value of the variable after the assignment. What you are really writing is code like:
int x;
x = 5;
System.out.println(x);
or
int x = 5;
System.out.println(x);
Most coding standards will prefer code written as shown above.
1