Java复习笔记(三)运算符

运算符

1.算数运算符

(1)+ (正数、加法、连接符)

 连接符的作用: 让任何的数据都可以与字符串进行拼接。
 如果+号用于字符串的时候,那么+号就是一个连接符,并不是做加法功能了。
 注意:任何类型的数据与字符串使用连接符连接,那么结果都是字符串类型的数据。

public class Main {
    public static void main(String[] args){
        System.out.println(1+2);
        System.out.println(1+2+3);
        System.out.println(1+"world");
        System.out.println(1+2+"world");
        System.out.println(1+"world"+2);
        System.out.println(1+2+3+"world"+1+2+3);
    }
}

//输出结果
3
6
1world
3world
1world2
6world123
(2)-(负号、减法)
public class Main {
    public static void main(String[] args){
        System.out.println(3-2);
        System.out.println(6-3-2);
    }
}
//输出结果
1
1
(3)*(乘法)
public class Main {
    public static void main(String[] args){
        System.out.println(2*3);
        System.out.println(2*3*4);
    }
}
//输出结果
6
24
(4)/ (除法)
public class Main {
    public static void main(String[] args){
        System.out.println(6/2);
        System.out.println(12/3/2);
    }
}
//输出结果
3
2
(5)% (取模、取余数)
public class Main {
    public static void main(String[] args){
        System.out.println(102%10);
        System.out.println(99999%2);
        System.out.println(-5%2);
        System.out.println(5.3%3);
        System.out.println(5%3.3);
    }
}
//输出结果
2
1
-1
2.3
1.7000000000000002
(6)++(自增)
public class Main {
    public static void main(String[] args){
        int a = 5;
        System.out.println(a++);
        System.out.println(a);
        int b = 5;
        System.out.println(++b);
        System.out.println(b);
    }
}
//输出结果
5
6
6
6

自增就是相当于操作数+1
后自增:++位于操作数的后面
后自增:先使用,后自增
前自增:++位于操作数的前面
前自增:先自增,后使用


后自增在jvm的运行原理:
因为后自增要使用没有+1之前的值,所以jvm会先声明一个变量用于保存没有+1之前的值。 int i = 0; i = temp; 原理: 1. int temp = i; // 声明了一个临时变量用于记录了i没有加1之前的值。 2. 自增。 i = i+1; i = 1; 3. temp把用作了表达式 的结果。 i的值发生了几次变化: i = 0 —–> 1—-> 0


(7)–(自减)
public class Main {
    public static void main(String[] args){
        int a = 5;
        System.out.println(a--);
        System.out.println(a);
        int b = 5;
        System.out.println(--b);
        System.out.println(b);
    }
}
//输出结果
5
4
4
4

其原理与自增相同

2.赋值运算符

(1)=
public class Main {
    public static void main(String[] args){
        int a = 5;
        int b = 10;
        a = b;
        System.out.println(a);
    }
}
//运行结果
10
(2)+=
public class Main {
    public static void main(String[] args){
        int a = 5;
        int b = 10;
        a += b;
        System.out.println(a);
    }
}
//运行结果
15
(3)-=
public class Main {
    public static void main(String[] args){
        int a = 10;
        int b = 5;
        a -= b;
        System.out.println(a);
    }
}
//运行结果
5
(4)*=
public class Main {
    public static void main(String[] args){
        int a = 2;
        int b = 3;
        a *= b;
        System.out.println(a);
    }
}
//运行结果
6
(5)/=
public class Main {
    public static void main(String[] args){
        int a = 10;
        int b = 5;
        a /= b;
        System.out.println(a);
    }
}
//运行结果
2
(6)%=
public class Main {
    public static void main(String[] args){
        int a = 13;
        int b = 3;
        a %= b;
        System.out.println(a);
    }
}
//运行结果
1

3.比较运算符

比较运算符: 比较运算符的结果都是返回一个布尔值的。

(1)== (判断是否等于)
public class Main {
    public static void main(String[] args){
        int a = 1;
        int b = 1;
        System.out.println(if(a==b));
        String str1 = "hello";
        String str2 = "hello";
        System.out.println(if(str1==str2));
    }
}
//运行结果
1
1

注意:
==用于比较两个基本数据类型数据的时候,比较的是两个变量所存储的值是否一致.
==用于比较两个引用类型变量的数据时候,比较的是两个引用类型变量所记录的内存地址是否一致.


(2)!= (判断是否不等于)
(3)> (判断是否大于)
(4)< (判断是否小于)
(5)>= (判断是否大于等于)
(6)<= (判断是否小于等于)

4.逻辑运算符

逻辑运算符的作用是用于连接布尔表达式的。

(1)& (与)

只有左右变量同时 为true,那么结果才是true,否则就为false

(2)| (或)

只要两边的布尔表达式有一边为true,那么结果就为true,只有两边同时为false的时候,结果才是false

(3)^ (异或)

只要两边的布尔表达式结果不一致,那么结果就为true,如果左右两边 的布尔表达式一致,那么就为false

(4)! (非)

单目运算符,右边为true,那么结果就为false,右边为false,那么结果就为true

(5) && (短路与)

短路与和与运算的结果一样,使用短路与的时候,当发现左边的布尔表达式为false时,则不会运算右边的布尔表达式。

(6) || (短路或)

短路或和或运算的结果一样,使用短路或的时候,当发现左边的布尔表达式为true时,则不会运算右边的布尔表达式。

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

        System.out.println(true|true);  // true
        System.out.println(true|false); // true
        System.out.println(false|true); // true
        System.out.println(false|false); // false

        System.out.println(true^true);  //  false
        System.out.println(true^false); //  true
        System.out.println(false^true); //  true
        System.out.println(false^false); //  false

        System.out.println(!true); // 

        System.out.println(true&&true);  //true
        System.out.println(true&&false); // false
        System.out.println(false&&true); // false
        System.out.println(false&&false); // false

        System.out.println(true||true);  // true
        System.out.println(true||false); // true
        System.out.println(false||true); // true
        System.out.println(false||false); // false
    }
}

5.位运算符

位运算符就是直接操作二进制位的。

(1)& (与)
(2)| (或)
(3)^ (异或)
(4)~ (取反)

注意: 如果操作数A连续异或同一个操作数两次,那么结果还是操作数A。
应用: 对数据加密.

public class Main {
    public static void main(String[] args){
        System.out.println(6&3); // 2 
        System.out.println(6|3); // 7
        System.out.println(6^3); // 5
        System.out.println(~7);  // -8
    }
}

6.移位运算符:

(1)<<(左移)

一个操作数进行左移运算的时候,结果就是等于操作数乘以2的n次方,n就是左移 的位数.

3<<1 = 3 *2(1) = 6;
3<<2 = 3*2(2) = 12
3<<3 = 3*2(3) = 24
(2)>>(右移)

一个操作数在做右移运算的时候,实际上就是等于该操作数除以2的n次方,n就是右移的位数。

3>>1 = 3 / 2(1) = 1
3>>2 = 3 / 2(2) = 0 。。
(3)>>>(无符号右移) :

无符号右移与右移的区别:进行右移运算的时候,如果操作数是一个正数,那么左边的空缺位使用0补,
如果操作数是一个负数,那么左边的空缺位使用1补。而使用无符号右移的时候,不管是正数还是负数都
统一使用0补。

public class Main {
    public static void main(String[] args){
        //左移
        System.out.println(3<<1); // 6 
        System.out.println(3<<2); // 12
        System.out.println(3<<3); // 24 
        //右移:
        System.out.println(3>>1); // 1 
        System.out.println(3>>>2);  //0 
    }
}

移位运算符应用场景:使用最高的效率算出2乘以8的结果。

    2<<3 = 2*2(3) = 16;

猜你喜欢

转载自blog.csdn.net/qq_29615991/article/details/79996566
今日推荐