Operators in Java

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

Java Five common operators:

Arithmetic operators, assignment operators, comparison operators, logical operators, and bitwise operators

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


one, arithmetic operators  

5 Kinds of binocular arithmetic operators: +,-,*,/,%

2 single-Mesh arithmetic operators: ++,--

Note: the "mesh" is the number of operation members representing the participating operations.

Monocular operator: means that only one operand is required to complete this type of operation.

Arithmetic operators
Describe
Instance Results
+ Sum 5+5
10
- Seeking Difference 5-4
1
*

Multiply two numbers

2*3
6
/

Divide two numbers

10/3 3
% Modulo (redundancy) 10%3
1
++ Self-increment (before, after)

-- Self-reduction (before, after)


Note:

1. (Self-increment, decrement) when the prefix mode is used, the increment or decrement operation is performed before the value of the expression is computed;

2. (Self-increment, decrement) when the suffix mode is used, the value of the expression is evaluated before the self-increment or decrement operation is performed;

3. Two number division (/) operation if it is two integers, the result will only show integers, also called rounding.

4. usually when writing an expression, for clarity of thought, parentheses are used even if the operator precedence is not changed, and parentheses must appear in pairs.



Two, assignment operators

Operator Describe Instance

=

Simple assignment operator, assigning a value from the right operand to the left operand

C = A + B

assigns a value of a + B to c

+= Add and assign operators, which add the right operand to the left operand and assign the result to the left operand

C + = A

Equivalent to C = C + A

-=

The right operand of the subtraction and assignment operators, minus the left operand and assigning the result to the left operand

C-= A

Equivalent to C = C-a

*=

The right operand of the multiplication and assignment operators, which will be associated with the left operand and assign the result to the left operand

C *= A

Equivalent to C = c * A

/=

Separator and assignment operators, which separate the left and right operands and assign the result to the left operand

C/= A

Equivalent to C = C/A

%=

Remainder and assignment operators, which use two operands to modulo and assign the result to the left number

C%= A

Equivalent to C = c% A

<<=

Left - shift and assignment operators

C <<= 2

Equivalent to C = C << 2

>>=

Right Shift and assignment operators

C >>= 2

Equivalent to C = C >> 2

&=

Bitwise operators and assignment operators

C &=

Equivalent to C = C & 2

^=

Bitwise XOR Operations and assignment operators

C ^= 2

Equivalent to C = c ^ 2

|=

bitwise OR and assignment operators

C |= 2

Equivalent to C = C | 2



Third, comparison operators (relational operators)

Operator

Describe
Instance

==

Checks whether the values of the two operands are equal or unequal, and if so, the condition becomes true.

(A = = B) is not true.

!=

Checks whether the values of the two operands are equal or unequal, and if the values are not equal, the condition becomes true.

(A! = B) is true.

>

Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition becomes true.

(A > B) is not true.

<

Checks if the value of the left operand is less than the value of the right operand, and if so, the condition becomes true.

(A < B) is true.

>=

Checks whether the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition becomes true.

(A >= B) is not true.

<=

Checks whether the value of the left operand is less than or equal to the value of the right operand, and if so, the condition becomes true.

(A <= B) is true.



Four, logical operators

Operator Describe Instance

&&

is called short circuit with.

(A && B) is false.

||

is called a short circuit or

(A | | B) is true.

!

is called the logical inverse

! (A && B) is true.

&

is called the logical AND operator.

(A & B) is false.

|

is called a logical OR operator.

(A | B) is true.



Five, bitwise operator

Operator Describe Instance

&

If it exists in two operands, the binary and operator will copy the result a little.

(A & B) would give which is 0000 1100

|

If it exists in any one of the operands, the binary or operator copies a bit.

(A | B) would give which is 0011 1101

^

Binary XOR or operation copy Point if it is set on an operand not.

(A ^ B) would give which is 0011 0001

~

The binary operator is the effect of a unary operator with a "flip" bit.

(~a) would give-61 which is 1100 0011 in 2 ' s complement form due to A signed binary number.

<<

The binary left shift operator. The value of the left operand is moved by the number of bits specified by the right operand.

A << 2 would give which is 1111 0000

>>

Binary right shift operator. The value of the left operand is moved by the number of bits specified by the right operand.

A >> 2 would give which is 1111

>>>

Shift right 0 fill operator. The value of the left operand is moved by the number of bits specified by the right operand, and the shifted value is padded with 0.

A >>>2 would give which is 0000 1111



Precedence of operators in Java

The so-called priority is the order of operations in an expression. The precedence of operators commonly used in Java is shown in the following table:

Priority level Operator
1 ()
2 ! + (positive)-(negative) + +--
3 * / %
4 + (plus)-(minus)
5 < <= > >=
6 == !=
7 ^
8 &&
9 ||
10
?:
11 = += -= *= /= %=



Other 1: String connector

"+" can also be used to concatenate strings In addition to arithmetic addition operations.

For example:

int i = 1 + 3;

String s = "Hello" + "world";

As long as one of the operands on either side of the "+" operator is a string type, the system automatically converts the second operand to a string and then connects .



Other 2: Conditional operators

The only one three-mesh operator.

The syntax is as follows: A=EXP1?EXP2:EXP3;

The value of A=EXP2 if the result of the EXP1 is true (that is, not 0); otherwise a=exp3 value

For example:

Big= (a>=b) a:b;

A>=b is true, then big=a, otherwise big=b

Operators in Java

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.