Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable assignment: int x = x = 1 [duplicate]

Tags:

java

This is allowed by the Java compiler, what is it doing?

int x = x = 1;

I realize that x is assigned to x, but how can it have two =s?

like image 520
Chris Smith Avatar asked Apr 26 '26 07:04

Chris Smith


1 Answers

x = 1 both assigns the value 1 to x and also 'returns' 1, it allows for things like this:

while ((line = reader.readLine()) != null)
like image 64
Karrde Avatar answered Apr 28 '26 21:04

Karrde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!