Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Other than readability, is there any value add if I use += operator like x+=y instead of x=x+y in java

Tags:

java

operators

I do understand that both += and var1=var1+var2 both do the same thing in Java. My question is, apart from better readability, what other benefit(if any) the former brings? Im asking because I was curious why java developers introduced this, they should have done it because of some value addition.

like image 830
Surya Chandra Avatar asked Nov 20 '25 17:11

Surya Chandra


1 Answers

It prevents the LHS of the + operation from being evaluated twice.

var.getSomething().x = var.getSomething().x + y;

This is not equivalent to

var.getSomething().x += y;

However, the added value from increased readability and reduced typing effort is not to be underestimated.

like image 149
Dietrich Epp Avatar answered Nov 23 '25 07:11

Dietrich Epp



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!