Java operator priority (from big to small) and associativity

Source: Internet
Author: User
Tags bitwise operators integer division

 

Operation

Priority

Associativity

1

Suffix Operator

[]. () (Function call)

Left to right

2

Single Object Operator

! ~ ++ -- + (Single-operand)-(single-operand)

From right to left

3

Create

New

Left to right

4

Multiplication and division

*/%

Left to right

5

Addition and subtraction

+-

Left to right

6

Shift

<>>>>

Left to right

7

Link

<<=>> = Instanceof

Left to right

8

Equal

=! =

Left to right

9

Bitwise AND

&

Left to right

10

Bitwise OR

^

Left to right

11

By bit or

|

Left to right

12

Logic and

&&

Left to right

13

Logic or

|

Left to right

14

Condition

? :

From right to left

15

Assignment

= + =-= * =/= %=^= <<=>=>>>>=

From right to left

Almost all operators can only operate on primitives ). The exceptions are "=", "=", and "! = ", They can operate on all objects. In addition, the string class supports "+" and "+ = ".

(1) Assignment

If "A = B" is used for the primary type, the content at "B" is copied to ". If a is modified, B is not affected by the modification.

The situation changes when the object is assigned a value. When operating on an object, what we actually operate on is its handle. Therefore, if a value is assigned "from one object to another", the handle is actually copied from one place to another. This means that if "c = D" is used for the object, C and D will eventually point to the object that was initially directed only by D.

Short S1 = 1; S1 = S1 + 1; (the S1 + 1 operation results in int type, which requires forced conversion)

Short S1 = 1; S1 + = 1; (correctly compiled) + = Operator no type conversion problem

(2) Arithmetic Operators

Java Arithmetic Operators: plus sign (+), minus sign (-), division sign (/), multiplication sign (*), and modulus (%, get the remainder from integer division ). Integer division directly removes decimal places rather than carry places.

(3) Auto increment and decrease

For the ascending and descending values (for example, ++ A or -- A), the operation is performed first and then the value is generated.

For post-incrementing and post-incrementing (such as a ++ or a --), the post-incrementing value is converted into a value and then executed.

(4) Relational operators

Relational operators include <,>, <=, >=, =, and ,! =

Equals and not equals apply to all built-in data types, but other comparisons do not apply to boolean types.

To compare whether the actual content of the two objects is the same, you must use the special method equals () applicable to all objects ().

The equals () method is not applicable to the "primary type". For those types, use = and! =.

By default, equals () is a comparison handle. So unless equals () is changed in our new class, it is impossible to show the expected behavior.

Most Java class libraries implement equals (), so they actually compare the content of objects rather than their handles.

= And! = Compare the object handle, not the actual content of the object

(5) logical operators

Logical operators &, | ,! Generate a Boolean value.

& Can be used as the logical operator "and", but & is "short circuit and". When calculating, you must first determine the value of the expression before the symbol. If you can determine the value of the entire expression, the operation of the expression after the symbol is not performed.

In addition, & can be used as bitwise operators

(6) bitwise operators

Bitwise AND operator (&)

Bitwise OR operator (|)

Bitwise XOR (^, exclusive or)

By bit not (~, It is also called the "Non" operator). It belongs to the unary operator and generates a value opposite to the input bit.

(7) Shift Operators

The Left shift operator (<) can move the operation object to the right of the operator (0 in the low position ).

The signed right shift operator (>) moves the operator object to the right to the specified number of digits on the right of the operator. The signed right shift operator uses symbol Extension: If the value is positive, 0 is inserted at the high position. If the value is negative, 1 is inserted at the high position.

Unsigned right shift operator (>>>), which uses "Zero extension": no matter whether it is positive or negative, 0 is inserted at a high position.

(8) ternary if-else Operator

Boolean expression? Value 0: if the value of 1 "Boolean expression" is true, the value 0 is calculated. Otherwise, the value 1 is calculated"

(9) string operator +

Int x = 0, y = 1, Z = 2;
System. Out. println ("Out:" + X + Y + Z );
Here, the Java compiler converts X, Y, and Z into their string forms, instead of adding them together first.

When "string +" is used, if the expression starts with a string, all subsequent computation objects will be converted to the string.

To connect a string using the plus sign (using an earlier Java version), make sure that the first element is a string.

(10) cast operator

For a narrowing conversion (data type that can accommodate more information, convert it to a smaller type, such as int to short ), in this case, information may be lost. At this time, the compiler will force us to make a clear shape

For "widening conversion", you do not need to make a clear shape, because the new type will certainly accommodate the original type of information and will not cause any information loss.

A boolean value (bollean) does not allow any styling at all, and any other primary type can shape each other.

After the float or double value is modeled as an integer, the fractional part is always cut off without any carry processing.

Math. Round (11.5) and so on? Math. Round (-11.5) and so on?

Math. Round (11.5) = 12 math. Round (-11.5) =-11

The round method returns a long integer closest to the parameter. After the parameter is added to 1/2, the floor is obtained.

(11) Priority

Operator (priority from low to high)

+-++-[[Rest...]

*/% +-<>

><>==! =

& | ^

A> B? X: Y

= (And compound assignment like * =)

Related Article

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.