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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章