更新時(shí)間:2022-06-22 12:13:40 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1790次
這些運(yùn)算符用于執(zhí)行邏輯“與”、“或”和“非”運(yùn)算,即類似于數(shù)字電子學(xué)中的與門和或門的功能。它們用于組合兩個(gè)或多個(gè)條件/約束或補(bǔ)充在特定考慮下對(duì)原始條件的評(píng)估。要記住的一件事是,如果第一個(gè)條件為假,則不會(huì)評(píng)估第二個(gè)條件,即它具有短路效應(yīng)。廣泛用于測(cè)試做出決定的幾個(gè)條件。讓我們?cè)敿?xì)了解每個(gè)邏輯運(yùn)算符:
當(dāng)考慮的兩個(gè)條件都滿足或?yàn)檎鏁r(shí),此運(yùn)算符返回真。如果兩者之一產(chǎn)生假,則運(yùn)算符的結(jié)果為假。例如,當(dāng) cond1 和 cond2 都為真(即非零)時(shí), cond1 && cond2 返回真。
句法:
條件1 && 條件2
插圖:
a = 10, b = 20, c = 20
條件1:a < b
條件2:b == c
如果(條件1 && 條件2)
d = a+b+c
// 因?yàn)閮蓚€(gè)條件都為真
d = 50。
例子
// Java code to illustrate
// logical AND operator
import java.io.*;
class Logical {
public static void main(String[] args)
{
// initializing variables
int a = 10, b = 20, c = 20, d = 0;
// Displaying a, b, c
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
System.out.println("Var3 = " + c);
// using logical AND to verify
// two constraints
if ((a < b) && (b == c)) {
d = a + b + c;
System.out.println("The sum is: " + d);
}
else
System.out.println("False conditions");
}
}
輸出:
變量 1 = 10
變量 2 = 20
變量 3 = 20
總和是:50
現(xiàn)在在下面的例子中,我們可以看到短路效應(yīng)。這里當(dāng)執(zhí)行到 if 語句時(shí),if 語句中的第一個(gè)條件為假,因此永遠(yuǎn)不會(huì)檢查第二個(gè)條件。因此 ++b(b 的預(yù)增量)永遠(yuǎn)不會(huì)發(fā)生并且 b 保持不變。
例子:
/*package whatever //do not write package name here */
import java.io.*;
class shortCircuiting {
public static void main (String[] args) {
// initializing variables
int a=10,b=20,c=15;
// displaying b
System.out.println("Value of b : "+b);
// Using logical AND
// Short-Circuiting effect as the first condition is false so the second condition is never reached and
// so ++b(pre increment) doesn't take place and value of b remains unchanged
if((a>c) && (++b>c)){
System.out.println("Inside if block");
}
// displaying b
System.out.println("Value of b : "+b);
}
}
輸出:

當(dāng)考慮的兩個(gè)條件之一滿足或?yàn)檎鏁r(shí),此運(yùn)算符返回真。如果兩者中的任何一個(gè)都為真,則運(yùn)算符的結(jié)果為真。要使結(jié)果為假,兩個(gè)約束都需要返回假。
句法:
條件1 || 條件2
例子:
a = 10, b = 20, c = 20
條件1:a < b
條件2:b > c
如果(條件1 || 條件2)
d = a+b+c
// 因?yàn)槠渲幸粋€(gè)條件為真
d = 50。
// Java code to illustrate
// logical OR operator
import java.io.*;
class Logical {
public static void main(String[] args)
{
// initializing variables
int a = 10, b = 1, c = 10, d = 30;
// Displaying a, b, c
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
System.out.println("Var3 = " + c);
System.out.println("Var4 = " + d);
// using logical OR to verify
// two constraints
if (a > b || c == d)
System.out.println("One or both"
+ " the conditions are true");
else
System.out.println("Both the"
+ " conditions are false");
}
}
輸出:
變量 1 = 10
變量 2 = 1
變量 3 = 10
變量 4 = 30
一個(gè)或兩個(gè)條件都為真
與前兩個(gè)不同,這是一個(gè)一元運(yùn)算符,當(dāng)考慮的條件不滿足或?yàn)榧贄l件時(shí)返回真?;旧?,如果條件為假,則操作返回真,當(dāng)條件為真時(shí),操作返回假。
句法:
!(健康)狀況)
例子:
a = 10, b = 20
!(a<b) // 返回 false
!(a>b) // 返回真
// Java code to illustrate
// logical NOT operator
import java.io.*;
class Logical {
public static void main(String[] args)
{
// initializing variables
int a = 10, b = 1;
// Displaying a, b, c
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
// Using logical NOT operator
System.out.println("!(a < b) = " + !(a < b));
System.out.println("!(a > b) = " + !(a > b));
}
}
輸出:
變量 1 = 10
變量 2 = 1
!(a < b) = 真
!(a > b) = 假
以上就是關(guān)于“帶有示例的Java邏輯運(yùn)算符介紹”,大家如果對(duì)此比較感興趣,想了解更多相關(guān)知識(shí),可以關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java邏輯運(yùn)算符,里面有更詳細(xì)的介紹等著大家去了解,希望對(duì)大家的學(xué)習(xí)能夠有所幫助。
相關(guān)閱讀
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)