Java 运算符


运算符应用在一个或多个运算数上,返回一个结果值。


运算符类型

Java语言有下面几种运算符:

  • 赋值运算符,包含=、+=、-=、*=、/=、%=、&=、|=、^=、>>=、<<=、>>>=。=,用于给变量赋值。其他+=、-=等都是+、-等运算符和赋值运算符两个运算符一起使用的简写形式。如x += y,等同于x = x + y。
  • 算术运算符,包含+、-、*、/、%,分别为加、减、乘、除、取余数运算符。
  • 一元运算符,包含+、-、++、--、!。一元运算符只需要一个运算数。+或-放在运算数前,表示该运算数为正数或负数。++和--用于将运算数的值增加或减少1。!用于对boolean型运算数取反。
  • 比较运算符,包含==、!=、>、>=、<、<=。比较运算符用于比较两个运算数的大小或是否相等。
  • 条件运算符,包含&&、||、?:。&&是与(and)运算符,当两个运算数都为true时,结果为true,否则为false。||是或(or)运算符,当两个运算数中至少有一个为true时,结果为true,否则为false。?:运算符是if-else的简化版本,共有3个运算数,格式为result = condition ? value1 : value2; condition是boolean类型的运算数,如果condition为true,result为value1的值,否则result为value2的值。
  • 类型比较运算符,instanceof。判断一个对象是否是一个类的实例。
  • 位运算符,包含~、&、|、^。~是反转运算符,对整型的每个bit,将0变成1,1变成0。&是按位与(and)运算符,对两个运算符的每个对应的bit,如果两个bit都为1,结果为1,否则为0。|是按位或(or)运算符,对两个运算符的每个对应的bit,如果两个bit至少有一个为1时,结果为1,否则为0。^是按位异或(xor)运算符,对两个运算符的每个对应的bit,如果两个bit有且只有一个为1,结果为1,否则为0。
  • 移位运算符(Bit Shift Operator),包含>>、<<、>>>。>>是有符号右移运算符,向右移动指定个数的bit后,用符号bit(最左边的bit)填充左边空出来的bit,向右移出的bit被丢弃。<<是有符号左移运算符,向左移动指定个数的bit后,右边空出来的bit用0填充。>>>是无符号右移运算符,向右指定个数的bit后,用0填充左边空出来的bit,向右移出的bit被丢弃。

赋值运算符

下面是赋值运算符的例子

public class Assignment {
    public static void main(String[] args) {
        int i = 5;
        int j = 6;
        int k = 2;
        int l = 12;
        int m = 13;
        System.out.println("i is " + i);    // 5
        i += 3;
        System.out.println("i += 3, i = " + i);    // 8
        System.out.println("j is " + j);    // 6
        j -= 2;
        System.out.println("j -= 2, j = " + j);    // 4
        System.out.println("k is " + k);    // 2
        k *= 3;
        System.out.println("k *= 3, k = " + k);    // 6
        System.out.println("l is " + l);    // 12
        l /= 5;
        System.out.println("l /= 5, l = " + l);    // 2
        System.out.println("m is " + m);    // 13
        m %= 5;
        System.out.println("m %= 5, m = " + m);    // 3

        // 整数的二进制表示
        int d =  0b1111_1111_1111_1111_1111_1111_0000_1111;
        int d2 = 0b1111_1111_1111_1111_1111_1111_1111_0000;
        System.out.println("d is           " + Integer.toBinaryString(d));            // 11111111111111111111111100001111
        System.out.println("d2 is         " + Integer.toBinaryString(d2));            // 11111111111111111111111111110000

        d &= d2;
        System.out.println("now d is " + Integer.toBinaryString(d));            // 11111111111111111111111100000000
    }
}

算术运算符

下面是算术运算符的例子

public class Arithmetic {
    public static void main(String[] args) {
        int i = 2 + 3;
        int j = 5 - 3;
        int k = 2 * 3;
        int l = 9 / 2;
        int m = 5 % 3;
        System.out.println("2 + 3 = " + i);    // 5
        System.out.println("5 - 3 = " + j);    // 2
        System.out.println("2 * 3 = " + k);    // 6
        System.out.println("9 / 2 = " + l);    // 4
        System.out.println("5 % 3 = " + m);    // 2
    }
}

一元运算符

下面是一元运算符的例子

public class Unary {
    public static void main(String[] args) {
        int i = + 3;
        int j = - 5;
        boolean b = true;
        boolean b2 = false;
        boolean notB = ! b;
        boolean notB2 = ! b2;

        System.out.println("i is " + i);    // 3
        int l = i ++;
        System.out.println("i ++ equals " + l);    // 3
        System.out.println("Now i is " + i);        // 4
        int m = ++ i;
        System.out.println("++ i equals " + m);    // 5
        System.out.println("Now i is " + i);        // 5

        System.out.println("j is " + j);    // -5
        int n = j --;
        System.out.println("j -- equals " + n);    // -5
        System.out.println("Now j is " + j);        // -6
        int p = -- j;
        System.out.println("-- j equals " + p);    // -7
        System.out.println("Now j is " + j);        // -7

        System.out.println("b is " + b);        // true
        System.out.println("! b equals " + notB);    // false
        System.out.println("b2 is " + b2);    // false
        System.out.println("! b2 equals " + notB2);    // true
    }
}

比较运算符

下面是比较运算符的例子

public class Comparison {
    public static void main(String[] args) {
        int i = 6;
        int j = 6;
        int k = 5;

        if (i == j)
            System.out.println(i + " equals " + j);    // 6 equals 6

        if (i > k)
            System.out.println(i + " is greater than " + k);    // 6 is greater than 5

        if (i >= j)
            System.out.println(i + " is greater than or equal to " + j);    // 6 is greater than or equal to 6

        if (k > i)
            System.out.println(k + " is less than " + i);

        if (i <= j)
            System.out.println(i + " is less than or equal to " + j);    // 6 is less than or equal to 6
    }
}

条件运算符

下面是条件运算符的例子

public class Condition {
    public static void main(String[] args) {
        System.out.println("true && true = " + (true && true));        // true
        System.out.println("true && false = " + (true && false));    // false
        System.out.println("false && false = " + (false && false));    // false

        System.out.println("true || true = " + (true || true));        // true
        System.out.println("true || false = " + (true || false));    // true
        System.out.println("false || false = " + (false || false));    // false

        int i = 2;
        String str = i > 3 ? i + " is greater than 3" : i + " is less than or equal to 3"; // 2 is less than or equal to 3
        System.out.println(str);
    }
}

&&和||运算符从左向右计算操作数,有两种情况会形成短路运算。false && operator2不会计算operator2的值,直接返回false(不论operator2的值是true或false,结果都为false,因此可以忽略计算operator2的值)。true || operator3不会计算oprator3的值,直接返回true(不论operator3的值是true或false,结果都为true)。

类型比较运算符

下面是类型比较运算符的例子

public class TypeComparison {
    public static void main(String[] args) {
        Pet pet = new Pet();
        Dog dog = new Dog();

        System.out.println("pet instanceof Dog: " + (pet instanceof Dog));    // false
        System.out.println("pet instanceof Pet: " + (pet instanceof Pet));    // true
        System.out.println("dog instanceof Dog: " + (pet instanceof Dog));    // false
        System.out.println("dog instanceof Pet: " + (pet instanceof Pet));    // true

        dog = null;
        System.out.println("null instanceof Dog: " + (dog instanceof Dog));    // false
    }
}

class Pet {
}

class Dog extends Pet {
}

null不是任何类的实例。

位运算符和移位运算符

下面是位运算符和移位运算符的例子

public class Bitwise {
    public static void main(String[] args) {
        int d =  0b1111_1111_1111_1111_1111_1111_0000_1111;
        int d2 = 0b1111_1111_1111_1111_1111_1111_1111_0000;
        System.out.println("d is           " + Integer.toBinaryString(d));            // 11111111111111111111111100001111
        System.out.println("d2 is         " + Integer.toBinaryString(d2));            // 11111111111111111111111111110000

        System.out.println("~ d =     " + Integer.toBinaryString((~ d)));                // 11110000

        System.out.println("d & d2 is " + Integer.toBinaryString(d & d2));            // 11111111111111111111111100000000
        System.out.println("d | d2 is     " + Integer.toBinaryString(d | d2));    // 11111111111111111111111111111111
        System.out.println("d ^ d2 is " + Integer.toBinaryString(d ^ d2));            // 11111111

        System.out.println("d is           " + Integer.toBinaryString(d));            // 11111111111111111111111100001111
        System.out.println("d >> 2 is " + Integer.toBinaryString(d >> 2));            // 11111111111111111111111111000011
        System.out.println("d << 2 is " + Integer.toBinaryString(d << 2));            // 11111111111111111111110000111100
        System.out.println("d >>> 2 is   " + Integer.toBinaryString(d >>> 2));    // 111111111111111111111111000011
    }
}