Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Operators

Java Operators

One of the most basic uses of computers is to perform mathematical operations. As a computer language, Java also provides a rich set of operators to manipulate variables. We can divide operators into the following groups:

Arithmetic operators

Arithmetic operators are used in mathematical expressions and function the same as in mathematics. The following table lists all arithmetic operators.

The examples in the table assume integer variable A has the value 10 and variable B has the value 20:

Operator Description Example
+ Addition - adds the values on both sides of the operator A + B equals 30
- Subtraction - subtracts the right operand from the left operand A - B equals -10
* Multiplication - multiplies the values on both sides of the operator A * B equals 200
/ Division - divides the left operand by the right operand B / A equals 2
% Modulus - the remainder of dividing the left operand by the right operand B % A equals 0
++ Increment: increases the value of the operand by 1 B++ or ++B equals 21 (see below for the difference)
-- Decrement: decreases the value of the operand by 1 B-- or --B equals 19 (see below for the difference)

Example

The simple example program below demonstrates arithmetic operators. Copy and paste the Java program below and save it as Test.java, then compile and run this program:

Example

public

class

Test

{

public

static

void

main

(

String

[

]

args

)

{

int

a

=

10

;

int

b

=

20

;

int

c

=

25

;

int

d

=

25

;

System

.

out

.

println

(

"

a + b =

"

(

a

b

)

)

;

System

.

out

.

println

(

"

a - b =

"

(

a

b

)

)

;

System

.

out

.

println

(

"

a * b =

"

(

a

b

)

)

;

System

.

out

.

println

(

"

b / a =

"

(

b

/

a

)

)

;

System

.

out

.

println

(

"

b % a =

"

(

b

%

a

)

)

;

System

.

out

.

println

(

"

c % a =

"

(

c

%

a

)

)

;

System

.

out

.

println

(

"

a++ =

"

(

a

++

)

)

;

System

.

out

.

println

(

"

a-- =

"

(

a

--

)

)

;

//

See the difference between d++ and ++d

System

.

out

.

println

(

"

d++ =

"

(

d

++

)

)

;

System

.

out

.

println

(

"

++d =

"

(

++

d

)

)

;

}

}

Run Example »

The compilation and execution results of the above example are as follows:


a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++   = 10
a--   = 11
d++   = 25
++d   = 27

Increment and decrement operators

1. Increment (++) and decrement (--) operatorsis a special arithmetic operator; while arithmetic operators require two operands to perform an operation, the increment and decrement operators use a single operand.

Example

public

class

selfAddMinus

{

public

static

void

main

(

String

[

]

args

)

{

int

a

=

3

;

//

Define a variable;

int

b

= ++

a

;

//

Increment operation

int

c

=

3

;

int

d

= --

c

;

//

Decrement operation

System

.

out

.

println

(

"

The value after the increment operation equals

"

b

)

;

System

.

out

.

println

(

"

The value after the decrement operation equals

"

d

)

;

}

}

The running result is:


进行自增运算后的值等于4
进行自减运算后的值等于2

Analysis:

**2. Prefix increment/decrement (++a, --a): ** Perform the increment or decrement first, then the expression evaluation.

**3. Postfix increment/decrement (a++, a--): ** Perform the expression evaluation first, then the increment or decrement. Example:

Example

public

class

selfAddMinus

{

public

static

void

main

(

String

[

]

args

)

{

int

a

=

5

;

//

Define a variable;

int

b

=

5

;

int

x

=

2

*++

a

;

int

y

=

2

b

++;

System

.

out

.

println

(

"

After prefix increment operation, a=

"

a

"

,x=

"

x

)

;

System

.

out

.

println

(

"

After postfix increment operation, b=

"

b

"

,y=

"

y

)

;

}

}

The running result is:


自增运算符前缀运算后a=6,x=12
自增运算符后缀运算后b=6,y=10

Relational operators

The following table lists the relational operators supported by Java

The examples in the table assume integer variable A has the value 10 and variable B has the value 20:

operator Description Example
== Checks whether the values of two operands are equal; if equal, the condition is true. (A == B) is false.
!= Checks whether the values of two operands are equal; if not equal, the condition is true. (A != B) is true.
>  Checks whether the value of the left operand is greater than the value of the right operand; if so, the condition is true. (A > B) is false.
<  Checks whether the value of the left operand is less than the value of the right operand; if so, the condition is true. (A
>= Checks whether the value of the left operand is greater than or equal to the value of the right operand; if so, the condition is true. (A >= B) is false.
<= Checks whether the value of the left operand is less than or equal to the value of the right operand; if so, the condition is true. (A <= B) is true.

Example

The simple example program below demonstrates relational operators. Copy and paste the Java program below and save it as Test.java, then compile and run this program:

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

[

]

args

)

{

int

a

=

10

;

int

b

=

20

;

System

.

out

.

println

(

"

a == b =

"

(

a

==

b

)

)

;

System

.

out

.

println

(

"

a != b =

"

(

a

!=

b

)

)

;

System

.

out

.

println

(

"

a > b =

"

(

a

b

)

)

;

System

.

out

.

println

(

"

a < b =

"

(

a

<

b

)

)

;

System

.

out

.

println

(

"

b >= a =

"

(

b

=

a

)

)

;

System

.

out

.

println

(

"

b <= a =

"

(

b

<=

a

)

)

;

}

}

The compilation and execution results of the above example are as follows:


a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false

Bitwise operators

Java defines bitwise operators that apply to integer types (int), long, short, char, and byte, etc.

Bitwise operators act on all bits and perform bit-by-bit operations. Assume a = 60, b = 13; their binary representations are as follows:


A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
~A= 1100 0011

The following table lists the basic bitwise operations, assuming integer variable A has the value 60 and variable B has the value 13:

Operator Description Example
& If corresponding bits are both 1, the result is 1; otherwise 0 (A & B) yields 12, which is 0000 1100
| If corresponding bits are both 0, the result is 0; otherwise 1 (A | B) yields 61, which is 0011 1101
^ If corresponding bits have the same value, the result is 0; otherwise 1 (A ^ B) yields 49, which is 0011 0001
~ The bitwise complement operator flips each bit of the operand, i.e., 0 becomes 1 and 1 becomes 0. (~A) yields -61, which is 1100 0011
<<  Bitwise left shift operator. The left operand is shifted left by the number of bits specified by the right operand. A << 2 yields 240, which is 1111 0000
>>  Bitwise right shift operator. The left operand is shifted right by the number of bits specified by the right operand. A >> 2 yields 15, which is 1111
>>>  Bitwise zero-fill right shift operator. The value of the left operand is shifted right by the number of bits specified by the right operand, and the vacated bits are filled with zeros. A >>> 2 yields 15, which is 0000 1111

Example

The simple example program below demonstrates bitwise operators. Copy and paste the Java program below and save it as Test.java, then compile and run this program:

Test.java File Code:

public

class

Test

{

 

public

static

void

main

(

String

[

]

args

)

{

   

int

a

=

60

;

/*

60 = 0011 1100

*/

      

int

b

=

13

;

/*

13 = 0000 1101

*/

    

int

c

=

0

;     

c

=

a

&

b

;      

/*

12 = 0000 1100

*/

    

System

.

out

.

println

(

"

a & b =

"

c

)

;     

c

=

a

|

b

;      

/*

61 = 0011 1101

*/

    

System

.

out

.

println

(

"

a | b =

"

c

)

;     

c

=

a

^

b

;      

/*

49 = 0011 0001

*/

    

System

.

out

.

println

(

"

a ^ b =

"

c

)

;     

c

= ~

a

;         

/*

-61 = 1100 0011

*/

    

System

.

out

.

println

(

"

~a =

"

c

)

;     

c

=

a

<<

2

;    

/*

240 = 1111 0000

*/

    

System

.

out

.

println

(

"

a << 2 =

"

c

)

;     

c

=

a

2

;    

/*

15 = 1111

*/

    

System

.

out

.

println

(

"

a >> 2  =

"

c

)

;      

c

=

a

2

;    

/*

15 = 0000 1111

*/

    

System

.

out

.

println

(

"

a >>> 2 =

"

c

)

;  

}

}

 

The compilation and execution results of the above example are as follows:


a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2  = 15
a >>> 2 = 15

Logical operators

The following table lists the basic logical operations, assuming Boolean variable A is true and variable B is false

Operator Description Example
&& called the logical AND operator. The condition is true only when both operands are true. (A && B) is false.
| | called the logical OR operator. If either of the two operands is true, the condition is true. (A || B) is true.
! called the logical NOT operator. It reverses the logical state of the operand. If the condition is true, the logical NOT operator yields false. !(A && B) is true.

Example

The simple example program below demonstrates logical operators. Copy and paste the Java program below and save it as Test.java, then compile and run this program:

Example

public

class

Test

{

public

static

void

main

(

String

[

]

args

)

{

boolean

a

=

true

;

boolean

b

=

false

;

System

.

out

.

println

(

"

a && b =

"

(

a

&&

b

)

)

;

System

.

out

.

println

(

"

a || b =

"

(

a

||

b

)

)

;

System

.

out

.

println

(

"

!(a && b) =

"

(

a

&&

b

)

)

;

}

}

The compilation and execution results of the above example are as follows:


a && b = false
a || b = true
!(a && b) = true

Short-circuit logical operators

When using the logical AND operator, the result is true only when both operands are true; but when the first operand is found to be false, the result must be false, and the second operand is not evaluated.

Example

public

class

LuoJi

{

public

static

void

main

(

String

[

]

args

)

{

int

a

=

5

;

//

Define a variable;

boolean

b

=

(

a

<

4

)

&&

(

a

++<

10

)

;

System

.

out

.

println

(

"

The result using the short-circuit logical operator is

"

b

)

;

System

.

out

.

println

(

"

The result of a is

"

a

)

;

}

}

The running result is:

使用短路逻辑运算符的结果为false
a的结果为5

Analysis: This program uses the short-circuit logical operator (&&); first it evaluates a<4 yields false, so b must be false, and the second operation a++ is no longer executed<10, so the value of a is 5.


Assignment operators

The following are the assignment operators supported by the Java language:

Operator Description Example
= Simple assignment operator, assigns the value of the right operand to the left operand C = A + B assigns the value of A + B to C
+ = Add and assignment operator, adds the left and right operands and assigns the result to the left operand C += A is equivalent to C = C + A
- = Subtract and assignment operator, subtracts the right operand from the left operand and assigns the result to the left operand C -= A is equivalent to C = C - A
* = Multiply and assignment operator, multiplies the left and right operands and assigns the result to the left operand C *= A is equivalent to C = C * A
/ = Divide and assignment operator, divides the right operand from the left operand and assigns the result to the left operand C /= A is equivalent to C = C / A when C and A are of the same type
(%)= Modulus and assignment operator, takes the modulus of the left and right operands and assigns the result to the left operand C %= A is equivalent to C = C % A
<< = 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 assignment operator C &= 2 is equivalent to C = C & 2
^ = Bitwise XOR assignment operator C ^= 2 is equivalent to C = C ^ 2
| = Bitwise OR assignment operator C |= 2 is equivalent to C = C | 2

Example

The simple example program below demonstrates assignment operators. Copy and paste the Java program below and save it as Test.java, then compile and run this program:

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

[

]

args

)

{

int

a

=

10

;

int

b

=

20

;

int

c

=

0

;

c

=

a

b

;

System

.

out

.

println

(

"

c = a + b =

"

c

)

;

c

+=

a

;

System

.

out

.

println

(

"

c += a  =

"

c

)

;

c

-=

a

;

System

.

out

.

println

(

"

c -= a =

"

c

)

;

c

*=

a

;

System

.

out

.

println

(

"

c *= a =

"

c

)

;

a

=

10

;

c

=

15

;

c

/=

a

;

System

.

out

.

println

(

"

c /= a =

"

c

)

;

a

=

10

;

c

=

15

;

c

%=

a

;

System

.

out

.

println

(

"

c %= a  =

"

c

)

;

c

<<=

2

;

System

.

out

.

println

(

"

c <<= 2 =

"

c

)

;

c

=

2

;

System

.

out

.

println

(

"

c >>= 2 =

"

c

)

;

c

=

2

;

System

.

out

.

println

(

"

c >>= 2 =

"

c

)

;

c

&=

a

;

System

.

out

.

println

(

"

c &= a  =

"

c

)

;

c

^=

a

;

System

.

out

.

println

(

"

c ^= a   =

"

c

)

;

c

|=

a

;

System

.

out

.

println

(

"

c |= a   =

"

c

)

;

}

}

The compilation and execution results of the above example are as follows:


c = a + b = 30
c += a  = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a  = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &= a  = 0
c ^= a   = 10
c |= a   = 10

Conditional operator (?:)

The conditional operator is also known as the ternary operator. It has 3 operands and needs to evaluate a boolean expression. Its main purpose is to decide which value should be assigned to a variable.


variable x = (expression) ? value if true : value if false

Example

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

[

]

args

)

{

int

a

,

b

;

a

=

10

;

//

If a equals 1, set b to 20; otherwise set it to 30

b

=

(

a

==

1

)

?

20

:

30

;

System

.

out

.

println

(

"

Value of b is :

"

b

)

;

//

If a equals 10, set b to 20; otherwise set it to 30

b

=

(

a

==

10

)

?

20

:

30

;

System

.

out

.

println

(

"

Value of b is :

"

b

)

;

}

}

The compilation and execution results of the above example are as follows:


Value of b is : 30
Value of b is : 20

instanceof operator

This operator is used to operate on object instances and check whether the object is of a specific type (class type or interface type).

The instanceof operator is used in the following format:


( Object reference variable ) instanceof  (class/interface type)

If the object referred to by the variable on the left side of the operator is an object of the class or interface (class/interface) on the right side of the operator, the result is true.

The following is an example:


String name = "James";
boolean result = name instanceof String; // 由于 name 是 String 类型,所以返回真

If the compared object is compatible with the type on the right, the operator still returns true.

See the example below:

class

Vehicle

{

}

public

class

Car

extends

Vehicle

{

public

static

void

main

(

String

[

]

args

)

{

Vehicle

a

=

new

Car

(

)

;

boolean

result

=

a

instanceof

Car

;

System

.

out

.

println

(

result

)

;

}

}

The compilation and execution results of the above example are as follows:

true

Java operator precedence

When multiple operators appear in an expression, which comes first? This involves the issue of operator precedence. In a multi-operator expression, different operator precedence can lead to very different final results.

For example, (1+3) + (3+2) * 2: if addition has the highest priority, the answer is 18; if multiplication has the highest priority, the answer is 14.

Another example, x = 7 + 3 * 2; here x gets 13, not 20, because the multiplication operator has higher precedence than the addition operator, so 3 * 2 is computed first to get 6, then 7 is added.

In the table below, the operators with the highest precedence are at the top of the table, and those with the lowest precedence are at the bottom.

Category Operator Associativity
Postfix () [] . (dot operator) Left to right
Unary expr++ expr-- Left to right
Unary ++expr --expr + - ~ ! Right to left
Multiplicative  * /% Left to right
Additive  + - Left to right
Shift  >> >>>  <<  Left to right
Relational  > >= < <=  Left to right
Equality  ==  != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR | | Left to right
Conditional ?: Right to left
Assignment = + = - = * = / =%= >> = << =&= ^ = | = Right to left
Comma , Left to right