The difference __java base between A+=b and A=a+b in Java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.