閱讀695 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Java 運算符

Java運算符其實沒什麼可以說的,隻需要按照數學上的內容進行即可.
但是有幾點還是需要進行理解和梳理的:
自增(++)自減(--)運算符
說白了就是在自己的基礎上進行加1或者減1;

class oprtator{
    public static void main(String[] args) {
        int i = 10;
        i++;//相當於:i = i+1
        System.out.println("i++等於:"+i);

        int b = 10;
        b--;//相當於:b=b-1
        System.out.println("i--等於: "+b);
    }
}

輸出:
i++等於:11
i--等於: 9
其實對於自增(++)自減(--)運算符還需要注意的是運算符的位置:

class oprtator2{
    public static void main(String[] args) {
        int i = 10;
        int b = i++;

        System.out.println("i= "+i);
        System.out.println("b= "+b);
        //當(++)為與運算值之後時,先把i的值賦給b 在進行++操作 所以b = 10  i = 11

        int c = 10;
        int d = c--;
        System.out.println("c= "+c);
        System.out.println("d= "+d);
        //同樣的 //當(--)為與運算值之後時,先把i的值賦給b 在進行--操作 所以d = 10  c= 9

        System.out.println("===========================我是分隔符===========================");

        int e = 10;
        int f = ++e;
        System.out.println("e= "+e);
        System.out.println("f= "+f);
        //當(++)位與運算值之前時,先進行++操作 再把e的值賦給f 在所以e =11   f = 11

        int g = 10;
        int h = --g;
        System.out.println("g= "+g);
        System.out.println("h= "+h);
        //同樣的 //當(--)為與運算值之後時,先進行--操作 ,在把的g值賦h 所以g = 9  h= 9

    }

輸出:
i= 11
b= 10
c= 9
d= 10
===========================我是分隔符===========================
e= 11
f= 11
g= 9
h= 9

邏輯運算符:

class oprtator3{
    public static void main(String[] args) {
        System.out.println("&& 隻有兩邊的值都為對的才輸出對的:====> " +(true && true));
        System.out.println("&& 隻有兩邊的值有一個為錯的就輸出錯的:====> " +(false && true));
        System.out.println();
        System.out.println("|| 隻有兩邊的值都為對時輸出對的:====> " +(true || true));
        System.out.println("|| 隻有兩邊的值隻要有一邊是對的就輸出對的:====> " +(false || true));
        System.out.println("|| 隻有兩邊的值都為錯的才輸出錯的:====> " +(false || false));
        System.out.println();
        System.out.println("! 運算結果為對的,則輸出錯的:====> " +(true || true));
        System.out.println("! 運算結果為錯的,則輸出對的:====> " +(false || false));
    }
}class oprtator3{
    public static void main(String[] args) {
        System.out.println("&& 隻有兩邊的值都為對的才輸出對的:====> " +(true && true));
        System.out.println("&& 隻有兩邊的值有一個為錯的就輸出錯的:====> " +(false && true));
        System.out.println();
        System.out.println("|| 隻有兩邊的值都為對時輸出對的:====> " +(true || true));
        System.out.println("|| 隻有兩邊的值隻要有一邊是對的就輸出對的:====> " +(false || true));
        System.out.println("|| 隻有兩邊的值都為錯的才輸出錯的:====> " +(false || false));
        System.out.println();
        System.out.println("! 運算結果為對的,則輸出錯的:====> " +(true || true));
        System.out.println("! 運算結果為錯的,則輸出對的:====> " +(false || false));
    }
}

輸出:
&& 隻有兩邊的值都為對的才輸出對的:====> true
&& 隻有兩邊的值有一個為錯的就輸出錯的:====> false

|| 隻有兩邊的值都為對時輸出對的:====> true
|| 隻有兩邊的值隻要有一邊是對的就輸出對的:====> true
|| 隻有兩邊的值都為錯的才輸出錯的:====> false

! 運算結果為對的,則輸出錯的:====> true
! 運算結果為錯的,則輸出對的:====> false

條件運算符(?:)
條件運算符也被稱為三元運算符;

class oprtator4{
    public static void main(String[] args) {
        int a = 1;
        int b = 1;
        String s = (a == b) ? "a等於b" : "a不等於b";
        System.out.println("(a == b)運算後內容為: "+s);

        int c = 1;
        int d = 2;
        String s1 = (c == d) ? "c等於d" : "c不等於d";
        System.out.println("(c == d)運算後內容為: "+s1);
    }
}

輸出:
(a == b)運算後內容為: a等於b
(c == d)運算後內容為: c不等於d

instanceof 運算符
instanceof 運算符使用來操作對象的,用於檢查某個對象是否屬於特定的Java類型或者Java接口;

/**
 * 水果類
 */
class Fruit{

}

/**
 * 蘋果類
 */
class Apple extends Fruit{

}

/**
 * 一直小貓
 */
class GreenCat extends Apple{

}

class oprtator5 {
    public static void main(String[] args) {
        Fruit fruit = new Fruit();
        Apple apple = new Apple();
        GreenCat cat = new GreenCat();

        /**
         * 判斷蘋果類是否屬於水果類
         */
    boolean falg =  apple instanceof Fruit;
    System.out.println(falg);

        /**
         * 判斷青蘋果屬不屬於水果
         */
    boolean falg1 = cat instanceof Fruit;
    System.out.println(falg1);
    }
}

輸入:
true
true
對於instanceof 的使用需要結合繼承\接口\抽象等等就AVVA類型來理解,這些內容後麵都會進行梳理.

最後更新:2017-05-23 21:01:50

  上一篇:go  《雲周刊》第123期:來了,致未來開發者,525首屆互聯網新兵進階在線峰會
  下一篇:go  MQC手遊行業解決方案詳解