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.


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
  • 2
    Its the return value of the assignment operator – user40980 Feb 27 '14 at 1:23
  • That is what I was looking for. After reading that SO question and looking at the JavaDocs I believe I have a better understanding. Thank you MichealT – medokr Feb 27 '14 at 1:30
  • There are a number of languages out there where everything is an expression that returns a value (even things like if-statements). It's actually quite a nice property to have in a language. – KChaloux Feb 27 '14 at 15:56
  • Note that the above would work in quite a lot of languages, syntax aside: C, C++, C#, JavaScript, Ruby... – haylem Feb 27 '14 at 16:51
  • @KChaloux its part of the core design of functional languages. Other languages have adopted some of the concepts from them (for example the functional-esque ?: ternary operator that is an if that returns a value). – user40980 Feb 27 '14 at 17:49

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

  • So interesting, I am intrigued by the while loop and looking forward to trying it out somewhere. – medokr Feb 27 '14 at 17:31

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 );
  • Currently I can't up vote which is frustrating. Thank you for posting a great answer as well. Showing what happens behind the scenes with the JVM helps to comprehend what is going on. You introduced me to POLA which I was unaware. Thanks! – medokr Feb 27 '14 at 17:38

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; 


int x = 5; 

Most coding standards will prefer code written as shown above.

  • I agree with your comment about coding standards. I was taking a quiz online and I didn't understand the syntax I asked about. I have not used that approach before. I don't plan to use that approach. Thank you for commenting. – medokr Feb 27 '14 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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