邏輯運算符 Logical Operator
邏輯運算符隻對佈爾型操作數進行運算並返回一個佈爾型數據。一共有6個邏輯運算符:&& , || ,& , | ,!和 ^
短路邏輯運算符 Short-Circuit Logical Operators:
public class Lesson04_6 {
02 public static void main(String[] args) {
03 int i = 5;
04 // 短路與運算符&&,要求左右兩個表達式都為true時才返回true,如果左邊第一個表達式為false時,它立刻就返回false,就好像短路瞭一樣立刻返回,省去瞭一些無謂的計算時間。
05 boolean flag = (i < 3) && (i < 4);
06 System.out.println(flag);
08 // 短路或運算符||,要求左右兩個表達式有一個為true時就返回true,如果左邊第一個表達式為true時,它立刻就返回true,就好像短路瞭一樣立刻返回,省去瞭一些無謂的計算時間。
09 flag = (i > 4) || (i > 3);
10 System.out.println(flag);
11 }
12 }
非短路邏輯運算符 Not Short-Circuit Operators:
view sourceprint?01 public class Lesson04_6 {
02 public static void main(String[] args) {
03 int i = 5;
04 // 非短路與運算符&,要求左右兩個表達式都為true時才返回true,兩個表達式他都會計算
05 boolean flag = (i < 3) & (i < 4);
06 System.out.println(flag);
08 // 非短路或運算符|,要求左右兩個表達式有一個為true時就返回true,兩個表達式他都會計算
09 flag = (i > 4) | (i > 3);
10 System.out.println(flag);
11 }
12 }
作者“藍花花的天空”