"Usage of operators in Java"

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators terminates true true

The Java operators can be divided into 4 classes: arithmetic operators, relational operators, logical operators, and bitwise operators.

1. Arithmetic operators
The arithmetic operators of Java are divided into unary and two-tuple operators. The unary operator has only one operand, the binary operator has two operands, and the operator is between two operands. The operand of an arithmetic operator must be a numeric type.

(1) Unary operators:
Unary operators are: positive (+), negative (-), plus 1 (+ +) and minus 1 (--) 4.
The plus 1, minus 1 operators allow only variables that are used for numeric types and are not allowed in an expression. The addition of 1, minus 1 operators can be placed before the variable (such as ++i), but also placed after the variable (such as i++), the difference is: if placed before the variable (such as ++i), the value of the variable is added 1 or minus 1, and then other appropriate operation (mainly assignment), if placed after the variable (such as i++) Then perform the other appropriate actions before you add 1 or minus 1 to the value of the variable.
For example:
int i=6,j,k,m,n;
j = +i; Take the original value, i.e. j=6
K =-I.; Take a negative value, i.e. 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
It is also important to note that there are no spaces allowed between the unary operators and the operands before or after the unary operator, or the compile error occurs.

(2) Two-dollar operator
Binary operators are: plus (+), minus (-), multiply (*), divide (/), take-up (%). where +,-, *,/complete add, subtract, multiply, divide arithmetic,% is to seek the remainder after dividing two operands.

% Redundancy Operation Example:
A% B = A-(A/b) * b
The take-up operator can be used for both operands that are integers, or for cases where two operands are floating-point numbers (or one operand is a floating-point number). When two operands are floating-point numbers, for example 7.6 2.9, the result is: 7.6-2 * 2.9 = 1.8.
When all two operands are of type int, the A%B is calculated as:
A% B = A-(int) (A/b) *
When all two operands are of type long (or other integer type), the A%B formula can be analogous.

When the data type of the two operands participating in the two-dollar operation is different, the resulting data type is the same as that of the higher-precision (or longer) data type.

For example:
7/3//divisible, operation result is 2
7.0/3//Division, operation result is 2.33333, that is, the result is consistent with the higher precision type
7% 3//take-up, operation result is 1
7.0% 3//take-up, operation result is 1.0
-7% 3//remainder, operation result is-1, that is, the symbol of the result of the operation is the same as the left operand
7%-3//remainder, operation result is 1, that is, the sign of the result of the operation is the same as the left operand

2. Relational operators
The relational operator is used to compare the size between two numeric values, which results in a numeric value of a logical type. There are six relational operators: equal to (= =), not equal to (! =), greater than (>), greater than or equal (>=), less than (<), less than or equal (<=).

For example:
9 <= 8//operation result is False
9.9 >= 8.8//operation result is True
' A ' < ' a '//the result of the operation is true, because the Unicode encoding value of the character ' a ' is less than the character ' a '
To illustrate that, for a greater than or equal to (or less than equals) relational operator, only a value greater than or equal to two of the relational operation is not valid, its result is false, as long as there is a (greater than or equal to) relationship operation established its result value is true. For example, for 9 <= 8,9 is neither less than 8 nor equal to 8, so the result of the operation of 9 <= 8 is false. For 9 >= 9, because 9 equals 9, the result of the operation of 9 >= 9 is true.

3. Logical operators
A logical operator requires that the data type of the operand be a logical type, and that the result of the operation is also a logical value. Logical operators are: Logic and (&&), logic, or (| | ), logical non (!), logical XOR (^), Logic and (&), logic, or (|).
The truth table is an intuitive way of representing the function of logical operations, which is to list all possible values of the logical operation in tabular form. The truth table for the Java language logical operators is as follows:
The truth table of logical operators
A B a&&b a| | B! A a^b a&b a| B
False to False false false true false
True false false true false
False true to true false true True
True True True True false false true

The first two columns are two logical variables that participate in the logical operation, there are 4 possible, so table 2.5 has 4 rows. The latter 6 columns are the result values of the 6 logical operators when the logical variable A and logical variable B take different values.
To illustrate, the two types of logic are basically the same as those for (&& and &), with two logical OR (| | and |) are basically the same arithmetic rules. The difference is that the:& and | Operations are all calculating the logical expression, and the && | | The operation has a short-circuit calculation function. The so-called short-circuit calculation, refers to the system from left to right logical expression calculation, once the calculation results have been determined, the calculation process is terminated. For the && operation, the end result is false, regardless of whether the value of the operator's right side is true or false, as long as the value of the left side of the operator is false. Therefore, once the system determines that the value of the left side of the && operator is false, the system terminates the subsequent calculation process; Operation, as long as the value of the left side of the operator is true, the end result is true regardless of whether the right side of the operator is true or false. So, once the system is judged | | The value of the left side of the operator is true, then the system terminates the subsequent evaluation process.

For example, you have the following logical expression:
(i>=1) && (i<=100)
At this point, if I equals 0, then the system determines that the i>=1 evaluates to False, the system immediately concluded that the final calculation of the logical expression is false, therefore, the system does not continue to judge the value of i<=100. The short-circuit calculation function can improve the speed of program operation.
Authors suggest readers: using && and | | In programming operator, do not use the & and | operator.

With logic (&&), logic, or (| | ) and logical non (!) can combine a variety of possible logical expressions. The logical expression is mainly used on the condition combination of the IF, while and other statements.
For example:
int i = 1;
while (i>=1) && (i<=100) i++; Cycle process
The loop procedure for the above program segment loops through the i++ statement 100 times.

4. Bitwise operators
Bitwise operations are operations performed in bits, with operands and results of integer values.
There are 7 bitwise operators, namely: Bit and (&), bit or (|), bit non (~), bit xor (^), right Shift (>>), left shift (<<), 0 padding right shift (>>>).
The bit of bitwise operation is identical to (&), bit or (|), bit non (~), Bit xor, or (^) the truth Table of the corresponding operation of the logical operation, the difference is that the operand and the result of the operation are all binary integers, and the operands and operation result of the corresponding operation of the logical operation are logical values.

Tip: The data types that the displacement operator applies to are Byte, short, int, long, char

displacement can be achieved by multiplying or dividing the integer by 2^n (2 of the n-th mean) effect. For example, Y<<2 (because the left shift is increased, full 2 in 1) so the left displacement of Y 2 is equivalent to Y*2^n

Bit Operation example
Operator name Example Description
& and X&y the X-and Y-bitwise
| Bits or X|y to place X and y as a bitwise OR
~ Bit non-~x the X bitwise
^ Bit XOR or x^y the X and y bitwise XOR
>> Move right x>>y the x to the right y position
<< left Shift X<<y X to the left Y position
>>> right Shift x>>>y X to the right Y-position, left to fill 0

To illustrate:
(1) The following procedure is available:
int x = 64; x equals 01000000 of binary number
int y = 70; Y equals 01000110 of the binary number
int z = x&y//z equals 01000000 of binary number
That is, the result of the operation is z equals the binary number 01000000. A bitwise OR, a bitwise non, a bitwise XOR, or a similar method of operation.
(2) The right shift is to shift a binary number to the right by the number of bits specified to move, to remove the dropped, to the left of the part to move in or to 0 (when the number is positive), or to 1 (when the number is negative). This is because integers use the complement notation inside the machine, the sign bit for positive numbers is 0, and the sign bit for negative numbers is 1. For example, for the following program segment:
int x = 70; x equals 01000110 of binary number
int y = 2;
int z = x>>y//z equals 00010001 of binary number
That is, the result of the operation is z equals the binary number 00010001, that is, Z equals the decimal number 17.
For the following program segment:
int x =-70; x equals 11000110 of binary number
int y = 2;
int z = x>>y//z equals 11101110 of binary number
That is, the result of the operation is z equals the binary number 11101110, that is, Z equals the decimal number-18. For a thorough understanding of right and left shifts, the reader needs to master the complement notation of the number of integers.
(3) 0 The right shift of the fill (>>>) is whether the moved number is positive or negative, the left side of the portion of the move is 0.

5. Other operators

(1) Simple use of assignment operators and other operators
The assignment operator can be combined with the two-tuple arithmetic operator, logical operator, and bitwise operator to simplify the writing of some common expressions.
How to use assignment operators and other operators in a simple way
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
*= s*=i s=s*i s,i is a numerical type
/= s/=i s=s/i s,i is a numerical type
%= s%=i s=s%i s,i is a numerical type
&= A&=b A=a&b A, B is a logical or integral type
|= A|=b a=a|b A, B is a logical or integral type
^= A^=b A=a^b A, B is a logical or integral type
<<= s<<=i s=s<<i s,i is an integral type
>>= s>>=i s=s>>i s,i is an integral type
>>>= s>>>=i s=s>>>i s,i is an integral type

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

(3) string plus (+) operator
When the operand is a string, the plus (+) operator is used to merge two strings, and when one side of the plus (+) operator is a string and the other side is a numeric value, the machine automatically converts the numeric value to a string, which is common in output statements. For the following program segment:
int max = 100;
System.out.println ("max =" +max);
The computer screen output is: max = 100, that is, the integer value 100 in the variable max is converted to the string 100 output.

(4) The conditional operator (? :)
The conditional operator (? :) in the syntax form:
< expression 1>? < expression 2>: < expression 3>
The conditional operator is evaluated by first calculating the value of the < expression 1>, and when the value of the < expression 1> is true, the value of the < expression 2> is used as the value of the entire expression, and when the < expression 1> the value is False, the < The value of the expression 3> as the value of the entire expression. Such as:
int A=1,b=2,max;
max = A>b? A:B; Max equals 2

(5) Coercion type conversion character
Coercion type conversions can cast the type of an expression to a specified data type, in the form of the following syntax:
(< type >) < expression >

(6) Object operator instanceof
The object operator instanceof is used to test whether a specified object is an instance of the specified class (or its subclasses), or False if True is returned.

(7) Dot operator
Dot operator "." Has two functions: one is to refer to a member of a class, and the other is to indicate the level of the package.

6. Precedence of Operators
The following lists all operators in the Java language in order of precedence from highest to lowest, and "left-right" in the binding column of the table indicates that the order of operations is left-to-right, and "right-left" indicates that the order of operations is right-to-left.

Precedence operator-binding
1. [] () ; ,
2 ++――+=! ~ + (one yuan)-(one yuan) right? Left
3 */% left? Right
4 + (two RMB)-(two yuan) left? Right
5 << >> >>> left/Right
6 < > <= >= instanceof left? Right
7 = = = = Left? Right
8 & Left? Right
9 ^ Left? Right
10 | Left? Right
One && left? Right
12 | | Left? Right
13: Right? Left
*=/=%= + = = <<= >>= >>>= &= ^= |= right? Left

Ternary operators

Ternary operators are equivalent to IF...ELSE statements

The ternary operators are available in the following formats:

Conditional type? Value 1: Value 2

The ternary operator has the following rule: If the value of the condition is true, the entire expression is evaluated as 1, otherwise the value is 2

"Usage of operators in Java"

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.