Common operators in Java and their order of precedence

Source: Internet
Author: User

Once you know the eight basic data types, it is necessary to understand the precedence of operators in use. Specific example:

It is important to note that the precedence of the coercion type conversion is in front of the multiplication and is behind the monocular operator, which is a relatively error-prone place. For example, use Math.random () to get an integer of 5 10 or less. Use the following code:

1 int [] a=newint[5]; 2  for (int i=0;i<5;i++) 3 {4     a[i]= (int) math.random () *10; 5 }6 System.out.println (Arrays.tostring (a));

At this point the console will output 5 0, because the coercion type conversion priority is higher than the multiplication, first rounding down and then the multiplication operation.

It is also important to note the "prefix" and "suffix" forms of the self-increment decrement operator, where the result is different when assigned or output, such as:

        int a=0;         int b=0;        System.out.println (a+ +);        System.out.println (++b);    

The output is 0, 1; This indicates that the "suffix" form is assigned first and then the self-increment operation, whereas the "prefix" form is the first self-increment operation and then the value is assigned.

Common operators in Java and their order of precedence

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.