/**
* //java中&&和&都是表示與的邏輯運算符,都表示邏輯運輸符and,當兩邊的表達式都爲true的時候,整個運算結果才爲true,否則爲false。
* //&&的短路功能,當第一個表達式的值爲false的時候,則不再計算第二個表達式;&則兩個表達式都執行。
* //&可以用作位運算符,當&兩邊的表達式不是Boolean類型的時候,&表示按位操作。
*
* @author tofacebook.com
*/
public class BooleanDemo {
public static void main(String[] args) {
// &&第一個表達式爲false 後面的就不執行了
int i = 0;
if(i == 3 && ++i > 0 ){ //當i== 3 爲 假時,++i 不執行
}
//System.out.println("i = " + i);
//console:i = 0 ==>第二個表達式沒有執行
// &第一個表達式爲false
int j = 0;
if(j == 3 & ++j > 0 ){//當 j == 3 不管爲真 還是假 ++j都會被執行。
}
//System.out.println("j = " + j);
//console:j = 1 ==>第二個表達式執行了
int x = 0;
if(x == 3 || ++x > 0 ){ //當 x ==3 爲假 時 後面的 ++x會被執行,當 x == 3 爲真時,後面的 ++x 不會別執行。
System.out.println("進來了 :x = " + x);
}
System.out.println("x = " + x);
//console:i = 0 ==>第二個表達式沒有執行
// &第一個表達式爲false
int y = 0;
if(y == 3 | ++y > 0 ){ //當 y == 3 不管爲真 還是假 ++y都會被執行。
System.out.println("進來了 : y = " + y);
}
System.out.println("y = " + y);
}
}