Java 运算符
算术、关系、位、逻辑、赋值与三元运算符的用法与优先级。
算术运算符
+ - * / % behave as in maths, but integer division truncates — make one operand floating point to keep the fraction. The sign of % follows the dividend.
public class Arithmetic {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
System.out.printf("%.2f%n", a / 3.0);
System.out.println(-10 % 3);
}
}13
7
30
3
1
3.33
-1关系运算符
== != > < >= <= return a boolean. Primitives compare values; references compare addresses — compare string content with equals().
public class Relational {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a < b);
String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}false
true
true
false
true位运算符
Bitwise operators work on binary digits: & and, | or, ^ xor, ~ not, << left shift, >> arithmetic right shift, >>> unsigned right shift. Common in permission flags and performance-sensitive code.
public class Bitwise {
public static void main(String[] args) {
int a = 6; // 0110
int b = 3; // 0011
System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);
System.out.println(~a);
System.out.println(a << 1);
System.out.println(a >> 1);
}
}2
7
5
-7
12
3逻辑运算符
&&, || and ! operate on booleans with short-circuit evaluation: && stops when the left side is false, || when it is true. That is how null guards work.
public class Logical {
static boolean side(String name) {
System.out.println("eval " + name);
return true;
}
public static void main(String[] args) {
System.out.println(false && side("right"));
System.out.println(true || side("right"));
System.out.println(true && side("right"));
}
}false
true
eval right
true赋值与三元运算符
Compound assignments (+=, -=, *=, /=, %=) cast implicitly, so short s = 1; s += 1; compiles while s = s + 1 does not. The ternary cond ? a : b is a compact if-else expression.
public class Assign {
public static void main(String[] args) {
int x = 10;
x += 5;
System.out.println(x);
short s = 1;
s += 1;
System.out.println(s);
int age = 20;
String type = age >= 18 ? "adult" : "minor";
System.out.println(type);
}
}15
2
adultinstanceof 运算符
obj instanceof Type tests whether an object is of that type (subclasses included). Since Java 16 the pattern form obj instanceof String s binds s when the test passes.
public class InstanceOf {
public static void main(String[] args) {
Object o = "hello";
System.out.println(o instanceof String);
System.out.println(o instanceof Integer);
if (o instanceof String s) {
System.out.println(s.length());
}
}
}true
false
5算术运算符
+ - * / % 与数学一致,但两个整数相除结果仍是整数(直接截断小数),想得到小数必须有一边是浮点。% 的结果符号跟随被除数。
public class Arithmetic {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
System.out.printf("%.2f%n", a / 3.0);
System.out.println(-10 % 3);
}
}13
7
30
3
1
3.33
-1关系运算符
== != > < >= <= 返回 boolean。基本类型比较的是值,引用类型比较的是地址 —— 想比较字符串内容必须用 equals()。
public class Relational {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a < b);
String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}false
true
true
false
true位运算符
位运算直接操作二进制位:& 与、| 或、^ 异或、~ 取反、<< 左移、>> 算术右移、>>> 无符号右移。常用于权限标志和性能敏感场景。
public class Bitwise {
public static void main(String[] args) {
int a = 6; // 0110
int b = 3; // 0011
System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);
System.out.println(~a);
System.out.println(a << 1);
System.out.println(a >> 1);
}
}2
7
5
-7
12
3逻辑运算符
&&、||、! 作用于布尔值,具有短路特性:&& 左边为 false 就不再算右边,|| 左边为 true 就短路。这常用于空值保护。
public class Logical {
static boolean side(String name) {
System.out.println("eval " + name);
return true;
}
public static void main(String[] args) {
System.out.println(false && side("right"));
System.out.println(true || side("right"));
System.out.println(true && side("right"));
}
}false
true
eval right
true赋值与三元运算符
复合赋值 += -= *= /= %= 会隐式做类型转换,short s = 1; s += 1; 合法而 s = s + 1 报错。三元运算符 条件 ? 值1 : 值2 是简化的 if-else 表达式。
public class Assign {
public static void main(String[] args) {
int x = 10;
x += 5;
System.out.println(x);
short s = 1;
s += 1;
System.out.println(s);
int age = 20;
String type = age >= 18 ? "adult" : "minor";
System.out.println(type);
}
}15
2
adultinstanceof 运算符
obj instanceof Type 判断对象是不是某个类型(含子类),返回 boolean。Java 16 起支持模式匹配 obj instanceof String s,判断成立时直接把 s 绑定好。
public class InstanceOf {
public static void main(String[] args) {
Object o = "hello";
System.out.println(o instanceof String);
System.out.println(o instanceof Integer);
if (o instanceof String s) {
System.out.println(s.length());
}
}
}true
false
5