Java 运算符

算术、关系、位、逻辑、赋值与三元运算符的用法与优先级。

Beginner
Key Points整数相除结果仍是整数;== 比较引用地址、equals 比较内容;&& 与 || 有短路特性。

算术运算符

+ - * / % behave as in maths, but integer division truncates — make one operand floating point to keep the fraction. The sign of % follows the dividend.

Java
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);
    }
}
Output
13
7
30
3
1
3.33
-1

关系运算符

== != > < >= <= return a boolean. Primitives compare values; references compare addresses — compare string content with equals().

Java
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));
    }
}
Output
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.

Java
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);
    }
}
Output
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.

Java
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"));
    }
}
Output
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.

Java
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);
    }
}
Output
15
2
adult

instanceof 运算符

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.

Java
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());
        }
    }
}
Output
true
false
5

Friends & Partners