Why do expressions print variable value using System.out.print?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật