Java and Java Operators

Source: Internet
Author: User

Java and Java Operators

1. Arithmetic Operators

The function is to calculate a number, including positive number +, negative number-, multiplication *, Division/, remainder %, addition +, subtraction-. The algorithm is the same as the calculation in mathematics.

Arithmetic OperatorsInstance(Assume that variable A = 10, variable B = 20 ):

Operator

Description

Example

+

ID

+ A = + 10 indicates positive number 10

-

Negative number

-B =-20 indicates a negative number of 20

+

Plus sign

A + B = 30

-

Minus sign

A-B =-10

*

Multiplication number

A * B = 200

/

Division Number

B/A = 2

%

Remainder

B % A = 0

 

2. bitwise operations

After the number is converted to the int type, the binary 0 is treated as false, and 1 is treated as true. Each bit performs a logical operation and the calculation result is the int type. Operators include: Bit non ~, Bitwise AND &, bitwise OR |, bitwise XOR or ^, shifted left <, shifted right >>, bitwise zeros shifted right >>>

Bitwise operationInstance(Assume that the variable A = 60 (binary is 00111100), the variable B = 13 (Binary Wei 00001101 )):

Operator

Description

Example

~

Non-bits: each bit of the flip operand

(~ A), get 11000011, that is, 195

&

Bitwise AND: This bitwise is 1 only when one of the two operands is not 0.

(A & B), get 00001100, that is, 12

|

Bitwise OR: if one of the two operands has a non-zero value, this bitwise is 1.

(A | B), get 00111101, that is, 61

^

Bitwise OR: if one of the two operands is not the same, change the bitwise to 1.

(A ^ B), get 00110001, that is, 49

<

Bitwise left shift: shifts the left operand by bitwise left the number specified by the right operand

A <2, get 11110000, that is, 240

>

Shifts right by bit: Shifts right by bit for the left operand

A> 2, get 00001111, that is, 15

>>>

Bitwise zeros shifted to the right: the value of the left operand is shifted to the right by the number of digits specified by the right operand, and the blank space obtained by moving is zero.

A> 2, get 00001111, that is, 15

Note:

1) shifts the bit to the left: Move the binary value of the int-type Integer to several places on the left, discard the bit that removes the first digit from the int range, and move the digit to the symbol bit, the bit that is left blank after several digits is supplemented with 0. If the symbol bit does not change, it is equivalent to the Npower of 2.
2) shifted right: shifts the binary value of an int integer to several places to the right, and removes the bit from the int range from the back to the right. All the bit values that are left blank in the front are signed digits. It is equivalent to the power of N except 2.
3) bitwise zeros shifted to the right: shifts the binary value of the int-type Integer to several places to the right, removes the bit from the int range, and removes all the bit values of the first bit to the right. A positive number is equal to the power of N except 2, and a negative number is changed to a positive number after the right shift.

 

3. string processing

Only one operator "+" is used to concatenate multiple strings into one large string.

String operation instance (variable A = "abc", variable B = 123 ):

Operator

Description

Example

+

Connector: concatenates multiple strings into one large string.

A + B = "ABC123"

 

4. relational operation

Judge the relationship between the two and return a Boolean value. Including: Judgment equal =, judgment unequal! =, Judgment greater than>, judgment greater than or equal to> =, judgment less than <, judgment less than or equal to <=.

Relational operatorsInstance(Assume that variable A = 10, variable B = 20 ):

Operator

Description

Example

=

Equality: Check whether the values of the two operands are equal. If they are equal, the condition is true.

(A = B) The result is false (not true)

! =

Not equal to: Check whether the values of the two operands are equal. If they are not equal, the condition is true.

(! A = B) The result is true.

>

If the value of the left operand is greater than that of the right operand, the condition is true.

(A> B) the result is false.

> =

Check whether the value of the left operand is greater than or equal to the right operand. If the value is greater than or equal to the right operand, the condition is true.

(A> = B) The result is false.

<

Smaller than: Check whether the value of the left operand is smaller than that of the right operand. If the value is smaller than, the condition is true.

(A <B) The result is true.

<=

If the value is less than or equal to: Check whether the value of the left operand is less than or equal to the value of the right operand. If the value is less than or equal to the value of the right operand, the condition is true.

(A <= B) The result is true.

 

5. logical operations

Returns a Boolean value for the composite operation on the link. Including: Logical !, Logic and &, logic or |, logic is different or ^.

Logical operation instance (assuming that Boolean A is true and B is false ):

Operator

Description

Example

!

Logical inversion: Used to reverse the logical state of an operand. If the condition is true, the logical non-operator returns false.

(! B) True

&

Logic and: the condition is true only when both operands are true. (True)

(A & B) is false

|

Logical or: when either of the two operands is true, the condition is true. (Full false)

(A | B) True

^

Logical variance or: The two operands are the same as false, and the difference is true.

(A ^ B) True

Appendix:

1. Logical short-circuit and, different from logic, is that once the result of the previous link is determined to be false, the subsequent link is not judged, and the result is directly returned to be false.
2. The logic is short-circuited or, which is different from the logic. Once the result of the previous link is true, the subsequent link is not judged, and the result is directly returned to be true.

 

6. Value assignment

Auto increment + +, auto increment one --, assign value =, auto increment * =, auto increment/=, auto increment % =, auto increment + =, auto increment-=, self and & = ,......

Assignment calculation instance:

Operator

Description

Example

++

Auto increment 1: the value of the operand is added to 1.

When B = 20, B ++ = 21

--

Auto-minus 1, and the operation value minus 1

When B = 20, B -- = 19

=

A simple value assignment operator assigns the value of the right operand to the left operand.

C = A + B will assign the value obtained by A + B to C

+ =

The addition and value assignment operator that adds the Left and Right operations to the left operations.

C + = A is equivalent to C = C +

-=

Subtraction and value assignment operator, which subtract the Left and Right operands and assign them to the left operands

C-= A is equivalent to C = C-A

* =

Multiplication and value assignment operator, which multiply the Left and Right operands and assign a value to the left operand

C * = A is equivalent to C = C *

/=

Division and value assignment operator, which separates the Left and Right operations and assigns the value to the left operations.

C/= A is equivalent to C = C/

% =

Modulo and value assignment operator. It modulo the Left and Right operands and assigns them to the left operands.

C % = A is equivalent to C = C %

<=

Left shift assignment operator

C <= 2 is equivalent to C = C <2

>>=

Right shift assignment operator

C> = 2 is equivalent to C = C> 2

& =

Bitwise AND value assignment operators

C & = 2 is equivalent to C = C & 2

^ =

Bitwise exclusive OR value assignment operator

C ^ = 2 is equivalent to C = C ^ 2

| =

Bitwise OR value assignment operator

C | = 2 is equivalent to C = C | 2

 

7. Conditional Operators

Logical judgment "? : ", Usage: variable x = (expression )? Value if true: value if false, logical judgment? Value 1: Value 2, used to determine whether the logic is true or false. If it is true, return 1; otherwise, return 2.

For example (1> 2 )? . Because 1> 2 is false, 4 is returned, I .e. (1> 2 )? The result of is 4.

 

 

 

Appendix: Java operator priority

When multiple operators appear in one expression, the priority of the operation is involved. In a multi-operator expression, different operation levels may cause a large difference in the result.

Sequence: arithmetic → bitwise → string → relational → logical → value assignment

Arithmetic: Positive minus sign> multiplication and division remainder> addition and subtraction;

Relational operation: Parallel

Logic: opposite> and> or/logical short-circuit or> logic exclusive or

Assignment: auto-increment, and auto-increment ......

In addition, you can enclose the priority value in parentheses.

 

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.