Java运算符包括:算术运算符、关系运算符、位运算符、逻辑运算符、赋值运算符、其他运算符。

算术运算符

即加减乘除、取余和自增自减,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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--) );
// 查看 d++ 与 ++d 的不同
System.out.println("d++ = " + (d++) );
System.out.println("++d = " + (++d) );
}
}

运行结果:

1
2
3
4
5
6
7
8
9
10
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

注意:

  1. 自增(++)自减(--)运算符是一种特殊的算术运算符,在算术运算符中需要两个操作数来进行运算,而自增自减运算符是一个操作数;
  2. 前缀自增自减法(++a, --a): 先进行自增或者自减运算,再进行表达式运算;
  3. 后缀自增自减法(a++, a--): 先进行表达式运算,再进行自增或者自减运算。

示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class selfAddMinus{
public static void main(String[] args){
int a = 3;
int b = ++a;
int c = 3;
int d = --c;
System.out.println("a=3, 进行自增运算后的值等于 "+b);
System.out.println("c=3, 进行自减运算后的值等于 "+d);

int u = 5;
int v = 5;
int x = 2*++u;
int y = 2*v++;
System.out.println("u=5, 自增运算符前缀运算后 u="+u+", x="+x);
System.out.println("v=5, 自增运算符后缀运算后 v="+v+", y="+y);
}
}

运行结果:

1
2
3
4
a=3, 进行自增运算后的值等于 4
c=3, 进行自减运算后的值等于 2
u=5, 自增运算符前缀运算后 u=6, x=12
v=5, 自增运算符后缀运算后 v=6, y=10

关系运算符

即关于判断大小关系的运算符。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
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) );
}
}

运行结果为:

1
2
3
4
5
6
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false

位运算符

Java位运算符用于整数类型(int),长整型(long),短整型(short),字符型(char),和字节型(byte)等类型。位运算符作用在所有的位上,并且按位运算。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 );
}
}

运行结果为:

1
2
3
4
5
6
7
a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2 = 15
a >>> 2 = 15

逻辑运算符

即与(&&)、或(||)、非(!)运算。示例如下:

1
2
3
4
5
6
7
8
9
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));
}
}

运行结果为:

1
2
3
a && b = false
a || b = true
!(a && b) = true

赋值运算符

即将以上的运算符与等号合并使用的运算符。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 );
}
}

运行结果为:

1
2
3
4
5
6
7
8
9
10
11
12
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

条件运算符(?:

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

instanceof运算符

该运算符用于操作对象实例,检查该对象是否是一个特定类型(类类型或接口类型)。

如果运算符左侧变量所指的对象,是操作符右侧类或接口(class/interface)的一个对象,那么结果为真。示例如下:

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

如果被比较的对象兼容于右侧类型,该运算符仍然返回true。示例如下:

1
2
3
4
5
6
7
8
9
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);
}
}

运行结果为:

1
true

参考链接

菜鸟教程:Java运算符