Introduction to Java Syntax (ii): operator

Source: Internet
Author: User
Tags bitwise empty integer division variables

1: Priority

Precedence means that multiple operators in the same equation are executed and ordered, and the operators at the same level have the same priority, and the operation symbols meet the same precedence, and the order of operations is determined by the binding, and the operators are listed below from highest to lowest precedence. Operators have the same precedence in the same row.

. [] () binding from left to right

+ +--! ~ Binding from left to right

*/% binding from left to right

+-binding from left to right

<< >> >>> binding from left to right

< > <= >= binding from left to right

= =!= binding from left to right

& binding from left to right

^ binding from left to right

&& binding from left to right

|| Binding from left to right

?: Binding from right to left

= binding from right to left

For example, for expressions: a=b+c*d/(c^d), because the parentheses have the highest precedence, the c^d is calculated first, then the C*d, then divided by C^d, and finally, the above results are stored with B and in variable A.

2: integer operator

The integer operator can be divided into one and two variables by the number of operands, the unary operator operates on one variable at a time, and the two-dollar operator operates on two variables at a time. For operations, if there is a variable or operand is a long integer, then the result is definitely a long integer, otherwise, even if the operand has not been determined to be a byte, short integer or character type, the result is an integer, the following table is a unary operator.

operator actual Operation example

-Change integer Symbol-I

~ Bit operation: Non-~i

+ + plus 1 i++

--minus 1 i--

Note: The unary negation operator (-) is used to change the positive and negative number of an integer, and the bitwise negation of all variables is 1 to 0, and the bit of 0 becomes 1, and Gaga and subtraction (++,--) Add the value of the variable to 1 or minus 1.

Here is a concrete example:

  int i=0;
   int j=1;
   for (i=1;i<10;i++)
   {
   j--;
   System.out.println(i+""+j+"");
   }

Unary operators are executed by changing the value of the variable they are acting on, the value of the variable is not changed for unary negation and bitwise negation, and the value of the quantity is changed for addition and subtraction (++,--), for example:

    int i=10,j=10,k=10,l=10;
     System.out.println(i+""+j+""+k+""+l+"");
     j++;
     i--;
     ~k;
     -l;
     System.out.println(i+""+j+""+k+""+l+"");

Note: J and I are changed and printed out their new values, but K and L are still the original values, and when you use a unary negation and a bitwise remainder operation in a compound expression, you are actually using a new value for the temporary storage operand.

+ + and--both the predecessors and the back operators, that is, they can either be placed before operands (++x) or later (x + +) if they are used in compound statements such as:

i=x++; or i=++x;

So in the first statement, X assigns the value to I and then adds 1, and the second statement first adds X 1 and then assigns the new X value to I.

The second type of integral operator is the two-dollar operator, which does not change the operand's value, but instead returns a value that must be assigned to the variable, and the following table lists the two-dollar operator.

operator actual Operation example

+ Add Operation A+b

-Subtraction Operation A-b

* Multiply Operation A*b

/except operation A/b

% modulo Operation A%b

& and Operation A&b

| or Operation A|b

^ Xor or op a^b

<< left Shift A<<b

>> Right Shift A>>b

>>> Right Shift A>>>b

Note that the basic function of the:>> and >>> is to move to the right, but >> is to use the symbol bit to fill the empty space left after the right shift, and >>> is to fill the empty space left by the zero padding after the right shift.

There is also a class of operations, which are shaped like: j-=i, where-= is a compound operation of two-and assignment-operations =, which is equivalent to J=j-i, which applies to all two-dollar operators.

Here are a few more details about integer operations, first of all, integer division is rounded to the direction near 0; second, if you divide by 0 or 0 modulo, the program will be forced to abort at run time, if your result exceeds the minimum limit, or underflow, the result will be 0, if the maximum limit is exceeded, will cause the result to wrap.

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.