Java language learning 5-operator 1

Source: Internet
Author: User

Value assignment operator

The value assignment operator is a combination from right to left, and the value of the table formula is the value on the right, for example:

 
Int A = 0; A = 3;

AboveCodeIn, the value of a is 3, and the value of expression A = 3 is also 3. Similar questions are often found in Java questions:

 
Int A = 0; int B = 5 + (A = 2 );

If we know that the expression A = 2 has a value equal to 2, the problem is very simple.

The four arithmetic operations +-*/% do not need to be explained. Note that only integer division is performed on integers. To get a decimal point value, convert the divisor or divisor to double.

However, the real problem lies not in the four arithmetic operations, but in Java, the floating point and single precision are not accurate because of the limited number of digits. Otherwise, there will be some bugs that are hard to detect.

For example:

 
Float a = 0.1f; float B = 0.2f; system. Out. println ("A + B =" + (a + B ));

The code above will print 0.3, no problem. However, if I increase the accuracy:

 
Double A = 0.1; Double B = 0.2; system. Out. println ("A + B =" + (a + B) + "! ");

At this time, the output will be "0.30000000000000004. This error is hard to find. Even Google's Android calculator has such a bug. Therefore, for decimal places, you must note that the expression of numbers is accurate. Do not directly compare them with floating point numbers, but use the absolute value of difference:

 
Double A = 0.3; Double B = 0.0; ...... if (math. Abs (B-a) <delta ){....}

In Java, all int and so on are signed numbers, so the highest bit represents the symbol, so you must pay attention to the overflow problem.

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.