1. 程式人生 > 其它 >短路運算+字串連線符

短路運算+字串連線符

短路運算:

public class Annotation {
    public static void main(String[] args) {
        int a = 5;
        boolean b = (a<4)&&(a++<5);//a<4已經成立,a++<5根本不
        //用再算了
        System.out.println(b);
        System.out.println(a);
    }
}

輸出結果:

false
5

位運算:

&、|、^、~、<<、>>:

A = 0011 1100

B = 0000 1101

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 1110

~B = 1111 0010

public class Annotation {
    public static void main(String[] args) {
        int A = 1;
        System.out.println(A<<3);
    }
}

輸出:

8

原因:A:0000 0001 左移三位→0000 1000

<<:*2
>>:/2
public class Annotation {
    public static void main(String[] args) {
        int A = 3;
        System.out.println(A<<3);
    }
}

輸出:

24

字串連線符:

public class Annotation {
    public static void main(String[] args) {
        int A = 3;
        int B = 4;
        System.out.println(A+B+"");
        System.out.println(""+A+B);
    }
}

輸出:

7
34
public class Annotation {
    public static void main(String[] args) {
        int A = 3;
        int B = 4;
        String type = A>B?"A>B":"A<B";
        System.out.println(type);
    }
}

輸出:

A<B