Java – the difference between a = B and a = a

In Java, I really want to know if there is a difference between using a = B; Or a = a, B Which one should I mainly use? I know the first one is a shortcut, but will the compiler get these two instructions in different ways?

Solution

See Java language specification, 15.26 2 Compound assignment operators

Reference to relevant parts:

short x = 3;
x += 4.6;
short x = 3;
x = (short)(x + 4.6);

So it's not just grammar sugar

int x = 1;
long a = 2l;
x+= a;

Compilation, where

int x =1;
long a =2l;
x = x+a;

A compilation error is given, as described here on stackoverflow quick recently

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>