Java学习笔记——运算符

目录

一、算术运算符

二、关系运算符

三、位运算符

四、逻辑运算符

五、赋值运算符

六、其他运算符

七、代码


 Java的运算符可分为以下几组:

算术运算符、关系运算符、位运算符、逻辑运算符、赋值运算符、其他运算符

Java学习笔记——运算符_第1张图片 一、算术运算符

代码实例 1

int a = 3;
int b = 5;

/* 算术运算符 */
System.out.println("a + b = " + (a + b));  // a + b = 3 + 5 = 8
System.out.println("a - b = " + (a - b));  // a - b = 3 - 5 = -2
System.out.println("a * b = " + (a * b));  // a * b = 3 * 5 = 15
System.out.println("b / a = " + (b / a));  // b / a = 5 / 3 = 1  "/"整除
System.out.println("b % a = " + (b % a));  // b % a = 5 % 3 = 2  "%"取余
System.out.println("a++ = " + (a++));  // a++ 自增,先进行表达式运算,再自增。因此输出为a = 3,之后a = a + 1 = 4
System.out.println("a-- = " + (a--));  // a-- 自减,先进行表达式运算,再自减。因此输出为a = 4,之后a = a - 1 = 3

输出结果1 

Java学习笔记——运算符_第2张图片

二、关系运算符

代码实例2

int a = 3;
int b = 5;

/* 关系运算符 */
System.out.println("a == b is " + (a == b));  // a == b  --> 3 != 5  -->  false
System.out.println("a != b is " + (a != b));  // a != b  --> 3 != 5  -->  true
System.out.println("a > b is " + (a > b));  // a > b -->  3 < 5  -->  false
System.out.println("a < b is " + (a < b));  // a < b -->  3 < 5  -->  true
System.out.println("b >= a is " + (b >= a));  // b >= a  -->  5 >= 3  -->  true
System.out.println("b <= a is " + (b <= a));  // b <= a  -->  5 >= 3  -->  fasle

输出结果2

Java学习笔记——运算符_第3张图片

三、位运算符

代码实例3

/* 位运算符 */
int c = 10;  // 二进制c -->  0000 1010
int d = 15;  // 二进制d -->  0000 1111
int num = 0;

// 对应位都是1,则1,否则0
num = c & d;
System.out.println("c & d is " + Integer.toBinaryString(num));
System.out.println("c & d is " + (num));
/*   0000 1010
   & 0000 1111
   ------------
     0000 1010
* */

// 存在1,则1,否则0
num = c | d;
System.out.println("c | d is " + Integer.toBinaryString(num));
System.out.println("c | d is " + (num));
/*   0000 1010
   | 0000 1111
   ------------
     0000 1111
* */

// 相反,则1,否则0
num = c ^ d;
System.out.println("c ^ d is " + Integer.toBinaryString(num));
System.out.println("c ^ d is " + (num));
/*   0000 1010
   ^ 0000 1111
   ------------
     0000 0101
* */

// 对应位取反
num = ~c;
System.out.println("~c is " + Integer.toBinaryString(num));
System.out.println("~c is " + (num));
/* ~ 0000 0000 0000 0000 0000 0000 0000 1010
   ------------------------------------------
     1111 1111 1111 1111 1111 1111 1111 0101
* */

// 向左移位,低位补0
num = d << 2;
System.out.println("d << 2 is " + Integer.toBinaryString(num));
System.out.println("d << 2 is " + (num));
/*   0000 1111
    ------------
     0011 1100
* */

// 向右移位,高位补0
num = d >> 2;
System.out.println("d >> 2 is " + Integer.toBinaryString(num));
System.out.println("d >> 2 is " + (num));
/*   0000 1111
   ------------
     0000 0011
* */

// 正数右移,高位补0;负数右移,高位补1
num = d >>> 2;
System.out.println("d >>> 2 is " + Integer.toBinaryString(num));
System.out.println("d >>> 2 is " + (num));
/*    0000 1111
    ------------
      0000 0011
* */

num = -d;
System.out.println("d is " + Integer.toBinaryString(num));
num = num >>> 2;
System.out.println("d >>> 2 is " + Integer.toBinaryString(num));
System.out.println("d >>> 2 is " + (num));
// 负数的二进制:先取反。再 + 1
/*   0000 0000 0000 0000 0000 0000 0000 1111         1111 1111 1111 1111 1111 1111 1111 0000
    -----------------------------------------  -->  -----------------------------------------
     1111 1111 1111 1111 1111 1111 1111 0000         1111 1111 1111 1111 1111 1111 1111 0001
* */

// 负数右移
/*   1111 1111 1111 1111 1111 1111 1111 0000
    -----------------------------------------
     0011 1111 1111 1111 1111 1111 1111 1100
* */

输出结果3

Java学习笔记——运算符_第4张图片

四、逻辑运算符

代码实例4 

/* 逻辑运算符 */
boolean b1 = true;
boolean b2 = false;

// && 全true,则true,否则false;  || 有一个true,则true,否则false;  !取反
System.out.println("b1 && b2 = " + (b1 && b2));
System.out.println("b1 || b2 = " + (b1 || b2));
System.out.println("!(b1 && b2) = " + !(b1 && b2));

输出结果4 

Java学习笔记——运算符_第5张图片

五、赋值运算符

代码实例5

/* 赋值运算符 */
final int x = 10;
int z1 = 1;
int z2 = 1;

z1 = z1 + x;
z2 += x;
System.out.println("z1 = z1 + x is: " + z1 + " and z2 += x is: " + z2);

z1 = z1 - x;
z2 -= x;
System.out.println("z1 = z1 - x is: " + z1 + " and z2 -= x is: " + z2);

z1 = z1 * x;
z2 *= x;
System.out.println("z1 = z1 * x is: " + z1 + " and z2 *= x is: " + z2);

z1 = z1 / x;
z2 /= x;
System.out.println("z1 = z1 / x is: " + z1 + " and z2 /= x is: " + z2);

z1 = z1 % x;
z2 %= x;
System.out.println("z1 = z1 % x is: " + z1 + " and z2 %= x is: " + z2);

z1 = z1 << 2;
z2 <<= 2;
System.out.println("z1 = z1 << 2 is: " + z1 + " and z2 <<= 2 is: " + z2);

z1 = z1 >> 2;
z2 >>= 2;
System.out.println("z1 = z1 >> 2 is: " + z1 + " and z2 >>= 2 is: " + z2);

z1 = z1 & x;
z2 &= x;
System.out.println("z1 = z1 & x is: " + z1 + " and z2 &= x is: " + z2);

z1 = z1 ^ x;
z2 ^= x;
System.out.println("z1 = z1 ^ x is: " + z1 + " and z2 ^= x is: " + z2);

z1 = z1 | x;
z2 |= x;
System.out.println("z1 = z1 | x is: " + z1 + " and z2 |= x is: " + z2);

输出结果5

Java学习笔记——运算符_第6张图片

六、其他运算符

1、条件运算符 (?:)

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

代码实例6 

// 条件运算符
int m = 10;
int n;
// 若m == 1为真,则n = 10,否则n = 20
n = (m == 1) ? 10: 20;
System.out.println("n: " + n);

// 若m == 10为真,则n = 10,否则n = 20
n = (m == 10) ? 10: 20;
System.out.println("n: " + n);

 输出结果6

2、instanceof运算符

  • instanceof运算符用于判断该对象是否是一个特定类型,是返回true,否则false。

代码实例7

// instanceof运算符
String name = "Jack";
System.out.println(name instanceof String);

输出结果7

七、代码

public class Day3 {
    public static void main(String[] args){
        int a = 3;
        int b = 5;

        /* 算术运算符 */
        System.out.println("a + b = " + (a + b));  // a + b = 3 + 5 = 8
        System.out.println("a - b = " + (a - b));  // a - b = 3 - 5 = -2
        System.out.println("a * b = " + (a * b));  // a * b = 3 * 5 = 15
        System.out.println("b / a = " + (b / a));  // b / a = 5 / 3 = 1  "/"整除
        System.out.println("b % a = " + (b % a));  // b % a = 5 % 3 = 2  "%"取余
        System.out.println("a++ = " + (a++));  // a++ 自增,先进行表达式运算,再自增。因此输出为a = 3,之后a = a + 1 = 4
        System.out.println("a-- = " + (a--));  // a-- 自减,先进行表达式运算,再自减。因此输出为a = 4,之后a = a - 1 = 3

        /* 关系运算符 */
        System.out.println("a == b is " + (a == b));  // a == b  --> 3 != 5  -->  false
        System.out.println("a != b is " + (a != b));  // a != b  --> 3 != 5  -->  true
        System.out.println("a > b is " + (a > b));  // a > b -->  3 < 5  -->  false
        System.out.println("a < b is " + (a < b));  // a < b -->  3 < 5  -->  true
        System.out.println("b >= a is " + (b >= a));  // b >= a  -->  5 >= 3  -->  true
        System.out.println("b <= a is " + (b <= a));  // b <= a  -->  5 >= 3  -->  fasle

        /* 位运算符 */
        int c = 10;  // 二进制c -->  0000 1010
        int d = 15;  // 二进制d -->  0000 1111
        int num = 0;

        // 对应位都是1,则1,否则0
        num = c & d;
        System.out.println("c & d is " + Integer.toBinaryString(num));
        System.out.println("c & d is " + (num));
        /*   0000 1010
           & 0000 1111
           ------------
             0000 1010
        * */

        // 存在1,则1,否则0
        num = c | d;
        System.out.println("c | d is " + Integer.toBinaryString(num));
        System.out.println("c | d is " + (num));
        /*   0000 1010
           | 0000 1111
           ------------
             0000 1111
        * */

        // 相反,则1,否则0
        num = c ^ d;
        System.out.println("c ^ d is " + Integer.toBinaryString(num));
        System.out.println("c ^ d is " + (num));
        /*   0000 1010
           ^ 0000 1111
           ------------
             0000 0101
        * */

        // 对应位取反
        num = ~c;
        System.out.println("~c is " + Integer.toBinaryString(num));
        System.out.println("~c is " + (num));
        /* ~ 0000 0000 0000 0000 0000 0000 0000 1010
           ------------------------------------------
             1111 1111 1111 1111 1111 1111 1111 0101
        * */

        // 向左移位,低位补0
        num = d << 2;
        System.out.println("d << 2 is " + Integer.toBinaryString(num));
        System.out.println("d << 2 is " + (num));
        /*   0000 1111
           ------------
             0011 1100
        * */

        // 向右移位,高位补0
        num = d >> 2;
        System.out.println("d >> 2 is " + Integer.toBinaryString(num));
        System.out.println("d >> 2 is " + (num));
        /*   0000 1111
           ------------
             0000 0011
        * */

        // 正数右移,高位补0;负数右移,高位补1
        num = d >>> 2;
        System.out.println("d >>> 2 is " + Integer.toBinaryString(num));
        System.out.println("d >>> 2 is " + (num));
        /*    0000 1111
            ------------
              0000 0011
        * */

        num = -d;
        System.out.println("d is " + Integer.toBinaryString(num));
        num = num >>> 2;
        System.out.println("d >>> 2 is " + Integer.toBinaryString(num));
        System.out.println("d >>> 2 is " + (num));
        // 负数的二进制:先取反。再 + 1
        /*   0000 0000 0000 0000 0000 0000 0000 1111         1111 1111 1111 1111 1111 1111 1111 0000
            -----------------------------------------  -->  -----------------------------------------
             1111 1111 1111 1111 1111 1111 1111 0000         1111 1111 1111 1111 1111 1111 1111 0001
        * */

        // 负数右移
        /*   1111 1111 1111 1111 1111 1111 1111 0000
            -----------------------------------------
             0011 1111 1111 1111 1111 1111 1111 1100
        * */

        /* 逻辑运算符 */
        boolean b1 = true;
        boolean b2 = false;

        // && 全true,则true,否则false;  || 有一个true,则true,否则false;  !取反
        System.out.println("b1 && b2 = " + (b1 && b2));
        System.out.println("b1 || b2 = " + (b1 || b2));
        System.out.println("!(b1 && b2) = " + !(b1 && b2));

        /* 赋值运算符 */
        final int x = 10;
        int z1 = 1;
        int z2 = 1;

        z1 = z1 + x;
        z2 += x;
        System.out.println("z1 = z1 + x is: " + z1 + " and z2 += x is: " + z2);

        z1 = z1 - x;
        z2 -= x;
        System.out.println("z1 = z1 - x is: " + z1 + " and z2 -= x is: " + z2);

        z1 = z1 * x;
        z2 *= x;
        System.out.println("z1 = z1 * x is: " + z1 + " and z2 *= x is: " + z2);

        z1 = z1 / x;
        z2 /= x;
        System.out.println("z1 = z1 / x is: " + z1 + " and z2 /= x is: " + z2);

        z1 = z1 % x;
        z2 %= x;
        System.out.println("z1 = z1 % x is: " + z1 + " and z2 %= x is: " + z2);

        z1 = z1 << 2;
        z2 <<= 2;
        System.out.println("z1 = z1 << 2 is: " + z1 + " and z2 <<= 2 is: " + z2);

        z1 = z1 >> 2;
        z2 >>= 2;
        System.out.println("z1 = z1 >> 2 is: " + z1 + " and z2 >>= 2 is: " + z2);

        z1 = z1 & x;
        z2 &= x;
        System.out.println("z1 = z1 & x is: " + z1 + " and z2 &= x is: " + z2);

        z1 = z1 ^ x;
        z2 ^= x;
        System.out.println("z1 = z1 ^ x is: " + z1 + " and z2 ^= x is: " + z2);

        z1 = z1 | x;
        z2 |= x;
        System.out.println("z1 = z1 | x is: " + z1 + " and z2 |= x is: " + z2);

        /* 其他运算符 */
        // 条件运算符
        int m = 10;
        int n;
        // 若m == 1为真,则n = 10,否则n = 20
        n = (m == 1) ? 10: 20;
        System.out.println("n: " + n);

        // 若m == 10为真,则n = 10,否则n = 20
        n = (m == 10) ? 10: 20;
        System.out.println("n: " + n);

        // instanceof运算符
        String name = "Jack";
        System.out.println(name instanceof String);
    }
}

你可能感兴趣的:(Java,java)