// 、| || 的区别
生活随笔
收集整理的這篇文章主要介紹了
// 、| || 的区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package day;
//&? &&? 、|? || 的區別?
public class Test_04 {
/*總結:|和|| 、&和&&的區別:
? ? & (長路與):無論左邊是true 還是 false; 右邊都運算
? ? && 當左邊為false時,右邊不運算?
? ? | (長路或):兩邊都參與運算
? ? || 當左邊為true 右邊不運算
? ??
? ? 注意:& && | || 最少也是由兩個表達式組成的 ;而 !非 一個表達式即可?
例如System.out.println(!('a'=='a'));//非真就是假 非假就是真
* */
public static void main(String[] args) {
//&長路與:無論第一個表達式的值是true或者false都會執行右邊
int i =2;
System.out.println(i==1 & i++ ==2);// 1 & 2
// false & true = false
System.out.println(i);//執行右邊;i++ = (2+1)3
System.out.println(i++ ==2);// 2 & 2 這里是做判斷不運算
// true & true = true?
System.out.println(i);//這里是運算后的結果,執行右邊;(i++ ==2)i=i+2——i=2+2——i=4
?
//&&短路與:只要第一個表達式的值為false,第二個表達式的值不在進行運行算
int j =2;
System.out.println(j==1 && j++ ==2);
//flase && true =false
System.out.println(j);//2? 左邊為false,右邊表達式值不需在運算
System.out.println(j==2 && j++ ==2);//j++只做替換成數字,不運算?
// true && true? =tuse? ?
System.out.println(j);//3? 左邊為真,右邊也要執行
//| 長路或:兩邊都參與運算
int a =1;
System.out.println(a==1 | a++ ==2);
// true? false | =true;??
System.out.println(a);//2? 左邊為真,右邊都要執行,結果輸出右邊的
int z =1;
System.out.println(z==2 | z++ ==1);
// false? true | =true;?
System.out.println(z);//2 左邊為假,右邊都要執行,結果輸出右邊的
//|| 短路或: 當左邊為true 右邊不運算? ? ?
int b =1;
System.out.println(b==1 || b++ ==2);
// true || false = true
}
}
//&? &&? 、|? || 的區別?
public class Test_04 {
/*總結:|和|| 、&和&&的區別:
? ? & (長路與):無論左邊是true 還是 false; 右邊都運算
? ? && 當左邊為false時,右邊不運算?
? ? | (長路或):兩邊都參與運算
? ? || 當左邊為true 右邊不運算
? ??
? ? 注意:& && | || 最少也是由兩個表達式組成的 ;而 !非 一個表達式即可?
例如System.out.println(!('a'=='a'));//非真就是假 非假就是真
* */
public static void main(String[] args) {
//&長路與:無論第一個表達式的值是true或者false都會執行右邊
int i =2;
System.out.println(i==1 & i++ ==2);// 1 & 2
// false & true = false
System.out.println(i);//執行右邊;i++ = (2+1)3
System.out.println(i++ ==2);// 2 & 2 這里是做判斷不運算
// true & true = true?
System.out.println(i);//這里是運算后的結果,執行右邊;(i++ ==2)i=i+2——i=2+2——i=4
?
//&&短路與:只要第一個表達式的值為false,第二個表達式的值不在進行運行算
int j =2;
System.out.println(j==1 && j++ ==2);
//flase && true =false
System.out.println(j);//2? 左邊為false,右邊表達式值不需在運算
System.out.println(j==2 && j++ ==2);//j++只做替換成數字,不運算?
// true && true? =tuse? ?
System.out.println(j);//3? 左邊為真,右邊也要執行
//| 長路或:兩邊都參與運算
int a =1;
System.out.println(a==1 | a++ ==2);
// true? false | =true;??
System.out.println(a);//2? 左邊為真,右邊都要執行,結果輸出右邊的
int z =1;
System.out.println(z==2 | z++ ==1);
// false? true | =true;?
System.out.println(z);//2 左邊為假,右邊都要執行,結果輸出右邊的
//|| 短路或: 當左邊為true 右邊不運算? ? ?
int b =1;
System.out.println(b==1 || b++ ==2);
// true || false = true
System.out.println(b);//1 左邊為真,右邊不需執行,結果輸出左邊的
int m =1;
System.out.println(m==2 || m++ ==1);
// false || true = true
System.out.println(m); //2 左邊為假,右邊依舊需執行
}
}
總結
以上是生活随笔為你收集整理的// 、| || 的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 赋值运算符 += 面试题小陷阱
- 下一篇: 练习5 键盘输入一个年份 判断年份 是否