Java syntax (2): Operators

Source: Internet
Author: User
Tags integer division
Java syntax introduction (2): Operator-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1: Priority

Priority refers to the execution and order of multiple operators in the same formula. operators in the same level have the same priority. operators with the same priority must meet each other. The operation order is determined based on the combination, the operators are listed from high to low. The operators in the same row have the same priority.


. [] () Associativity from left to right
++ --! ~ Associativity from left to right
*/% Associativity from left to right
+-Associativity from left to right
<>>>> Associativity from left to right
<><=> = Associativity from left to right
=! = Associativity from left to right
& Associativity from left to right
^ Associativity from left to right
& Associativity from left to right
| Associativity from left to right
? : Associativity from right to left
= Associativity from right to left

For example, for an expression: a = B + c * d/(c ^ d), because parentheses have the highest priority, c ^ d is calculated first, followed by c * d, divide the result by c ^ d and store the sum of the preceding result and B in variable.

2: integer Operators

Integer operators can be divided into one and two classes based on the number of operands. The unary operator operates on a variable at a time and the binary operator operates on two variables at a time. For an operation, if a variable or operand is of a long integer type, the result must be of a Long Integer type. Otherwise, even if the operand is not determined to be of the byte type, short Integer type, or dense type, the calculation result is an integer type. The following table lists the unary operators.

Actual operator operation example
-Change the integer-I
~ Bit operation: Non ~ I
++ Plus 1 I ++
-- Minus 1 I --

Note: The unary operator (-) is used to change the positive and negative numbers of integers. bitwise inversion converts all the bits of the variable being 1 to 0, and the bitwise of 0 to 1, add and subtract (++, --) Add 1 or minus 1 to the variable value.
The following is an example:

Int I = 0;
Int j = 1;
For (I = 1; I <10; I ++)
{
J --;
System. out. println (I + "" + j + "");
}

The unary operator is executed to change the value of the variables they are used for. For The unary and bitwise inversion, the value of the variable is not changed, but the addition and subtraction operations (++, --), the amount value is changed, 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 their new values are printed, but k and l are still original values, when a compound expression uses the unary round and bitwise remainder operations, you are actually using a new value of a temporary storage operand.

++ And -- both pre-and post-operators, that is, they can be placed before the operands (++ x) or after (x ++ ), if they are used in compound statements, such:

I = x ++; or I = ++ x;
Then, in the first statement, x assigns the value to I and then adds 1. In the second statement, x adds 1 and then the new x value to I.
The second type of integer operators is binary operators. This operation does not change the value of the operand, but returns a value that must be assigned to the variable. The following table lists binary operators.

Actual operator operation example
+ Addition operation a + B
-Subtraction a-B
* Multiplication operation a * B
/Division operation a/B
% Modulo operation a % B
& Operation a & B
| Or operation a | B
^ Exclusive or operation a ^ B
<Move a left < > Right shift a> B
>>> Right shift a >>> B


Note: the basic functions of >>and >>are shifted to the right, but >> is to use the symbol bit to fill the space left after the right shift, >>> is the space left after the right shift is filled with zero.

There is another type of operation, such as j-= I. Here,-= is a compound operation of binary operation and value assignment operation =, which is equivalent to j = j-I, this composite method applies to all binary operators.

Here are some further explanations about integer operations. First, integer division is rounded to the direction near 0. Second, if you divide by 0 or modulo 0, the program will be forcibly aborted during running. If your calculation result exceeds the minimum limit or overflows, the result will be 0. If the maximum limit is exceeded, this will cause the result to be rolled back.

3: boolean operator

There are also some operators that can generate Boolean results. We call them boolean operators. The following table lists these operators.

Actual operator operation example
<Less than > Greater than a> B
<= Less than or equal to a <= B
> Greater than or equal to a> = B
= Equal to a = B
! = Not equal to! = B

Here we should point out that equal operator = may bring you a lot of trouble. Many people often use equal sign = when comparing two values, which leads to a value assignment operation, so in programming, you must note that you use = when comparing values. There are several logical operators in the boolean type. They can perform operations on the boolean data :!, &&,//

In fact, if you think of the Boolean value true as 1, false as 0, then for the same operator, the rule of Integer Operation is also applicable to the boolean type.

There is also a ternary operator in the Boolean operator. Its format is as follows:

Operand? Statement1: Statement2
In this formula, the true and false Operand values are calculated first. If true, Statement1 is executed. If false, Statement2 is executed. The following code provides an example of this operation:

(A> B )? A: B;
This expression returns the larger value in a and B.

4: Floating Point operators

In addition to the value assignment operator (+ =,-=. * =. /) modulo (%) and modulo value (% = 0) Make the floating point value equal to the integer division. Similarly, ++ and -- increase or decrease the value of the variable by 1.

Similar to integer variables, the result is placed in the maximum length type. If the calculation includes two floating point types, the result is also a floating point type. If one or more of them are Double Precision Floating Point types, the result is a double-precision floating point. When you use Relational operators >,<, >=, <=, ==, and! =, Pay attention to the difference between floating point variables and integer types. Only a = B is true and cannot describe B is not true. This is because the order of floating point values is different from that of integer values. When you write a program and make assumptions about floating point values, be careful. floating point values include inf, which indicates infinity, overflow generates infinite inf, and underflow generates 0.

5: String Operators

+ Operators can combine strings. If an operand is not a string, it is converted to a string before the concatenation. In addition, + = Operator puts the result of the two strings in the first string. In the previous example, when you want to print several items in the same row, the + operator is used, try to use System in the interpreter. out. println and + operators are used to generate different output combinations.

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.