Java operators are loaded from the Network)

Source: Internet
Author: User

Java operators can be classified into four types: Arithmetic Operators, Relational operators, logical operators, and bitwise operators.

1. Arithmetic Operators
Java Arithmetic Operators include unary operators and binary operators. The unary operator has only one operand. The binary operator has two operands, which are located between two operands. The operands of arithmetic operators must be numerical values.

(1) unary operator:
Unary operators include: positive (+), negative (-), plus 1 (++), and minus 1.
The addition and subtraction operators can only be used for numeric variables, but cannot be used in expressions. 1. The subtraction operator can be placed before a variable (such as ++ I) or after a variable (such as I ++). The difference between the two is: if it is placed before the variable (for example, ++ I), the variable value is first added with 1 or minus 1, and then other corresponding operations (mainly value assignment operations ); if it is placed after the variable (such as I ++), perform other operations first, and then add or subtract 1 to the variable value.
For example:
Int I = 6, J, K, M, N;
J = + I; // obtain the original value, that is, j = 6
K =-I; // negative value, that is, K =-6
M = I ++; // first M = I, then I = I + 1, that is, M = 6, I = 7
M = ++ I; // first I = I + 1, then M = I, that is, I = 7, M = 7
N = j --; // first n = J, then J = J-1, that is, n = 6, j = 5
N = -- J; // first J = J-1, then n = J, that is, j = 5, n = 5
Note that space is not allowed between the unary operator and the operations before and after it; otherwise, an error occurs during compilation.

(2) binary Operators
Binary operators include addition (+), subtraction (-), multiplication (*), Division (/), and remainder (% ). Here, the plus, minus, multiplication, and Division operations are completed. % is the remainder of the two operands after division.

Examples of % remainder operations:
A % B = A-(A/B) * B
The remainder operator can be used when both operands are integers or when both operands are floating-point (or one operand is floating-point. When both operands are floating-point numbers, for example, 7.6% 2.9, the calculation result is: 7.6-2*2.9 = 1.8.
When both operands are of the int type, the formula for calculating a % B is:
A % B = A-(INT) (a/B) * B
When both operands are of the long type (or other integer type), the calculation formula of a % B is similar.

When the data types of the two operands involved in the binary operation are different, the Data Type of the result is the same as that of the Data Type with higher precision (or longer digits.

For example:
7/3 // division, the calculation result is 2
7.0/3 // division, the calculation result is 2.33333, that is, the result is consistent with the type with high accuracy.
7% 3 // obtain the remainder and the calculation result is 1
7.0% 3 // obtain the remainder and the calculation result is 1.0
-7% 3 // obtain the remainder and the operation result is-1. That is, the symbol of the operation result is the same as that of the left operand.
7%-3 // obtain the remainder and the operation result is 1, that is, the symbol of the operation result is the same as that of the left operand.

2. Relational operators
Relational operators are used to compare the values between two values. The calculation result is a logical value. There are six Relational operators: equal to (=) and not equal (! =), Greater than (>), greater than or equal to (> =), less than (<), less than or equal to (<= ).

For example:
9 <= 8 // The operation result is false.
9.9> = 8.8 // The calculation result is true.
'A' <'A' // The calculation result is true, because the unicode encoded value of 'A' is smaller than that of 'A'
It must be noted that, for a relational operator greater than or equal to (or less than or equal to), The result value is false only when both relational operations are not successful, if there is a (greater than or equal to) relational operation, its result value is true. For example, 9 <= 9 is neither less than 8 nor 8, so the 9 <= 8 calculation result is false. For 9> = 9, because 9 is equal to 9, 9> = 9 returns true.

3. logical operators
Logical operators require that the data type of the operands be logical, and the calculation result is also logical. Logical operators include: logical and (&), logical or (|), logical non (!) , Logical exclusive or (^), logical and (&), logical or (| ).
A truth table is an intuitive way to express the logic operation function. The specific method is to list all possible values of a logical operation in the form of a table. The truth table of logical operators in Java is as follows:
Truth Table of logical operators
A B a & B a | B! A A ^ B a & B a | B
False false True False false
True False false True False true
False True False true false true
True true false true

The first two columns are two logical variables involved in logical operations. There are four possibilities, so Table 2.5 has four rows. The last six columns are the calculation result values of the six logical operators when the logical variables A and B take different values.
It should be noted that the two logics are basically the same as the (&) operation rules, and the calculation rules of the two logics or (| and |) are also basically the same. The difference is: & and | the operation is to calculate all the logical expressions, while & and | the operation has the short-circuit calculation function. Short-circuit calculation refers to the calculation of logical expressions from left to right. Once the calculation result is determined, the calculation process is terminated. For & Operations, if the value on the left side of the operator is false, the final result is false regardless of whether the value on the right side of the operator is true or false. Therefore, once the system determines that the value of the left end of the & operator is false, the system terminates the subsequent calculation process. For | operations, if the value of the left end of the operator is true, the final result is true regardless of whether the right side of the operator is true or false. Therefore, once the system determines |
If the value on the left side of the operator is true, the system terminates the subsequent calculation process.

For example, the logical expression is as follows:
(I> = 1) & (I <= 100)
At this time, if I is equal to 0, the system determines that the result of I> = 1 is false, and the system immediately obtains that the final calculation result of this logical expression is false. Therefore, the system does not continue to judge the value of I <= 100. The short-circuit computing function can improve the program running speed.
The author recommends that you use the & | Operator in programming without the & and | Operator.

Use logic and (&), logic or (|), and Logic Non (!) A variety of possible logical expressions can be combined. Logical expressions are mainly used in condition combinations of if, while, and other statements.
For example:
Int I = 1;
While (I> = 1) & (I <= 100) I ++; // cyclic process
The loop process of the above program segment cyclically executes the I ++ statement 100 times.

4. bitwise operators
Bitwise operations are performed in units of binary bits. The operands and calculation results are integer values.
There are 7 bitwise operators: bitwise AND (&), bitwise OR (|), bitwise NOT (~) , Bitwise exclusive or (^), right shift (>), left shift (<), right shift of 0 fill (>>> ).
Bitwise operations: bitwise AND (&), bitwise OR (|), bitwise non (~) The bitwise OR (^) is exactly the same as the true value table of the corresponding logical operation. The difference is that the operands and results of bitwise operation are both binary integers, the operations and results of logical operations are logical values.

Bitwise operation example
Operator name example
& Bitwise and x & Y calculate the bitwise sum of X and Y
| Bit or X | y calculates X and Y by bit or
~ Bit non ~ X calculates the non-bitwise value of X
^ Bitwise variance or x ^ y returns the bitwise variance or of X and Y
> Shift right x> Y shifts the values of X to the right of Y.
<Shift left x <y shifts the values of X to the Y position.
>>> Shift right X >>> y shifts all the values of X to the right, and fill in 0 on the left.

Example:
(1) There are the following sections:
Int x = 64; // X is equal to 01000000 of the binary number
Int y = 70; // y equals 01000110 of the binary number
Int z = x & Y // Z equals 01000000 of the binary number
That is, the result is Z equal to 01000000 binary numbers. Bitwise OR, bitwise non, bitwise exclusive or similar operation methods.
(2) The right shift is to shift a binary number to the right based on the specified number of digits. The removed binary number is discarded, and the left part is moved in or supplemented with 0 (when this number is correct ), or fill in 1 (when this number is negative ). This is because the integer uses the complement representation inside the machine. The positive sign bit is 0, and the negative sign bit is 1. For example, for the following section:
Int x = 70; // X equals 01000110 of the binary number.
Int y = 2;
Int z = x> Y // Z equals 00010001 of the binary number.
That is, the calculation result is Z equal to the binary number 00010001, that is, z is equal to the decimal number 17.
For the following sections:
Int x =-70; // X equals 11000110 of the binary number.
Int y = 2;
Int z = x> Y // Z equals 11101110 of the binary number.
That is, the calculation result is Z equal to the binary number 11101110, that is, z is equal to the decimal number-18. To thoroughly understand the right-shift and left-shift operations, you need to understand the complement representation of integer machine numbers.
(3) The right shift of the 0 padding (>>>) indicates that no matter whether the number to be moved is positive or negative, the left-side shifted part will be filled with 0.

5. Other operators

(1) simple use of value assignment operators and other operators
The value assignment operator can be combined with Binary Arithmetic Operators, logical operators, and bit operators to form a simple operator, which simplifies the writing of some common expressions.
Simple usage of value assignment operators and other operators
Operator usage is equivalent to description
+ = S + = I s = S + I s, I is a numeric type
-= S-= I s = s-I s, I is a numeric type
* = S * = I s = S * I s, I is a numeric type
/= S/= I s = s/I s, I is a numeric type
% = S % = I s = S % I s, I is Numeric
& = A & = B a = A & B a, B is a logical or integer
| = A | = B a = A | B a, B is a logical or integer
^ = A ^ = B a = a ^ B a, B is a logical or integer
<= S <= I s = S <I s, I is an integer
>>=S >>= I S = s> I s, I is an integer
>>>> = S >>>= I s = s >>> I s, I is an integer

(2) square brackets [] and parentheses () Operators
Square brackets [] are array operators, and the values in square brackets [] are the subscript of the array. The entire expression represents the element value of the position where the subscript is located in the array.
The parentheses () operator is used to change the priority of operators in an expression.

(3) string plus (+) Operator
When the operand is a string, the plus (+) operator is used to merge two strings. When one side of the plus (+) operator is a string and the other side is a value, the machine automatically converts a value to a string, which is common in output statements. For example:
Int max = 100;
System. Out. println ("max =" + max );
The output result of the computer screen is: max = 100, that is, the integer 100 in the max variable is converted to the string 100 for output.

(4) conditional operators (? :)
Conditional operators (? :) Syntax format:
<Expression 1>? <Expression 2>: <expression 3>
The conditional operator is used to calculate the value of <expression 1>. If the value of <expression 1> is true, the value of <expression 2> is used as the value of the entire expression. When the value of <expression 1> is false, the value of <expression 3> is used as the value of the entire expression. For example:
Int A = 1, B = 2, Max;
Max = A> B? A: B; // Max equals 2

(5) force type conversion character
The force type conversion operator can forcibly convert the type of an expression to a specified data type. Its Syntax format is:
(<Type>) <expression>

(6) object operator instanceof
The object operator instanceof is used to test whether a specified object is an instance of a specified class (or its subclass). If so, true is returned; otherwise, false is returned.

(7) vertex Operators
The dot operator "." has two functions: one is a member of the Reference class, and the other is a hierarchical level of the package.

6. Operator priority
The following lists all operators in the Java language in the order of priority from high to low. The "Left and Right" in the combiner column of the table indicates that the operation order is from left to right, "Right left" indicates that the operation order is from right to left.

Combination of priority Operators
1. [] ();,
2 ++-+ =! ~ + (One dollar)-(one dollar) Right left
About 3 */%
4 + (Binary)-(Binary) left and right
5 <>>>> left and right
6 <> <=> = about instanceof
7 =! = Left and right
8 & around
About 9 ^
10 | around
11 & around
12 | left and right
13? : Right left
14 = * =/= % = + =-= <<=>>>>>=<=^= | = right left

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.