There is a difference between a+=b and a=a+b in the Java language, and the main difference is in the accuracy of the operation, of course =, *=,/=,%= is also a truth. Here take A+=b and a=a+b as an example to illustrate.
(1) Take a section of Java program for example, try a+=b form:
Public Classdemo
{
public static void Main (string[] args)
{
Short a=4;
a+=5;
System.out.println (a);
}
}
It can be seen that the result of this program is 9, the program is not a problem. It needs to be clear that the "+ +" operator is an operator in Java, not two, and that the program automatically converts data type to high precision when it executes "+ =". Therefore, the above program in the implementation of the compiler did not error.
(2) Try the A=a+b form below:
Public Classdemo
{
public static void Main (string[] args)
{
Short a=4;
a=a+5;
System.out.println (a);
}
}
You can see from the screenshot that there was a problem with the form of a=a+b, because A is a short type of variable, and 5 is a variable of type int by default, and the short type +int type results are automatically converted to int type.
There is a problem of inconsistent data types, with the type of variable to save the type int. Variable compiler of course it will be an error, just like you use one or two water cups to install 22 water,
Of course, can not fit, if you have to install one or two water cups to install 22 water, it will certainly be hard to go in, but certainly would spill a part of the water. So if you're going to use the type of a variable of type int, you can use a cast to determine if the precision is lost.
The following is the result of the cast:
Public Classdemo
{
public static void Main (string[] args)
{
Short a=4;
A= (short) (a+5);
System.out.println (a);
}
}
(3) Summing up a sentence, the main difference between the two forms is whether the automatic conversion of data types, when the two operands of the same type of the two forms of operation results are no difference, when two operand data types are not the same,
And the left operand is the data precision of the operand that holds the result is low, at this time the two forms are only different.