Java中&和&&以及|和||的區(qū)別、應(yīng)用場(chǎng)景和代碼示例
前言
在Java中,& 和 && 以及 | 和 || 都是邏輯運(yùn)算符,但它們?cè)谑褂蒙嫌幸恍┲匾膮^(qū)別。以下是對(duì)這些運(yùn)算符的全面總結(jié),包括它們的區(qū)別、應(yīng)用場(chǎng)景和代碼示例。
1. & 和 &&
- &:
- 按位與運(yùn)算符: 當(dāng)作用于整數(shù)類型時(shí),它執(zhí)行按位與操作。
- 邏輯與運(yùn)算符: 當(dāng)作用于布爾類型時(shí),它會(huì)計(jì)算兩邊的表達(dá)式,無(wú)論左邊的表達(dá)式是否為 false。
- &&:
- 短路與運(yùn)算符: 當(dāng)作用于布爾類型時(shí),如果左邊的表達(dá)式為 false,則不會(huì)計(jì)算右邊的表達(dá)式,直接返回 false。
代碼示例
public class Main { public static void main(String[] args) { boolean a = true; boolean b = false; // 使用 & boolean result1 = a & checkCondition(); // 輸出: Result with &: false System.out.println("Result with &: " + result1); // 使用 && boolean result2 = a && checkCondition(); // 輸出: Result with &&: false System.out.println("Result with &&: " + result2); // 使用 &,即使左邊為 false,右邊的表達(dá)式仍然會(huì)被計(jì)算 boolean result3 = b & checkCondition(); // 輸出: Result with &: false System.out.println("Result with &: " + result3); // 使用 &&,左邊為 false 時(shí),右邊的表達(dá)式不會(huì)被計(jì)算 boolean result4 = b && checkCondition(); // 輸出: Result with &&: false System.out.println("Result with &&: " + result4); } public static boolean checkCondition() { System.out.println("Checking condition"); return false; } }
2. | 和 ||
- |:
- 按位或運(yùn)算符: 當(dāng)作用于整數(shù)類型時(shí),它執(zhí)行按位或操作。
- 邏輯或運(yùn)算符: 當(dāng)作用于布爾類型時(shí),它會(huì)計(jì)算兩邊的表達(dá)式,無(wú)論左邊的表達(dá)式是否為 true。
- ||:
- 短路或運(yùn)算符: 當(dāng)作用于布爾類型時(shí),如果左邊的表達(dá)式為 true,則不會(huì)計(jì)算右邊的表達(dá)式,直接返回 true。
代碼示例
public class Main { public static void main(String[] args) { boolean a = true; boolean b = false; // 使用 | boolean result1 = a | checkCondition(); // 輸出: Result with |: true System.out.println("Result with |: " + result1); // 使用 || boolean result2 = a || checkCondition(); // 輸出: Result with ||: true System.out.println("Result with ||: " + result2); // 使用 |,即使左邊為 true,右邊的表達(dá)式仍然會(huì)被計(jì)算 boolean result3 = b | checkCondition(); // 輸出: Result with |: false System.out.println("Result with |: " + result3); // 使用 ||,左邊為 true 時(shí),右邊的表達(dá)式不會(huì)被計(jì)算 boolean result4 = b || checkCondition(); // 輸出: Result with ||: true System.out.println("Result with ||: " + result4); } public static boolean checkCondition() { System.out.println("Checking condition"); return false; } }
3. 為什么要使用 & 和 | 而不是總是使用 && 和 ||
雖然 && 和 || 具有短路特性,能夠在很多情況下提高效率和安全性,但在某些特定場(chǎng)景下,& 和 | 也有其獨(dú)特的優(yōu)勢(shì):
- 確保兩邊表達(dá)式都被計(jì)算:
- 記錄日志: 即使第一個(gè)條件不滿足,也希望記錄第二個(gè)條件的檢查結(jié)果。
- 更新多個(gè)狀態(tài): 即使第一個(gè)狀態(tài)更新失敗,也希望繼續(xù)更新其他狀態(tài)。
- 多個(gè)副作用操作: 每個(gè)操作都有一定的副作用,希望確保所有操作都執(zhí)行。
- 多個(gè)輸入驗(yàn)證: 即使第一個(gè)輸入驗(yàn)證失敗,也希望繼續(xù)驗(yàn)證其他輸入字段
代碼示例
public class Main { public static void main(String[] args) { // 記錄日志 boolean condition1 = checkCondition1(); boolean condition2 = checkCondition2(); boolean result1 = condition1 & condition2; System.out.println("Final result: " + result1); // 更新多個(gè)狀態(tài) boolean status1 = updateStatus1(); boolean status2 = updateStatus2(); boolean result2 = status1 & status2; System.out.println("Final result: " + result2); // 多個(gè)副作用操作 boolean operation1 = performOperation1(); boolean operation2 = performOperation2(); boolean result3 = operation1 & operation2; System.out.println("Final result: " + result3); // 多個(gè)輸入驗(yàn)證 boolean isValid1 = validateInput1(); boolean isValid2 = validateInput2(); boolean result4 = isValid1 & isValid2; System.out.println("Final result: " + result4); } public static boolean checkCondition1() { System.out.println("Checking condition 1"); return true; } public static boolean checkCondition2() { System.out.println("Checking condition 2"); return false; } public static boolean updateStatus1() { System.out.println("Updating status 1"); return true; } public static boolean updateStatus2() { System.out.println("Updating status 2"); return false; } public static boolean performOperation1() { System.out.println("Performing operation 1"); return true; } public static boolean performOperation2() { System.out.println("Performing operation 2"); return false; } public static boolean validateInput1() { System.out.println("Validating input 1"); return true; } public static boolean validateInput2() { System.out.println("Validating input 2"); return false; } }
總結(jié)
- && 和 || 主要用于布爾邏輯運(yùn)算,具有短路特性,能夠提高效率和安全性。
- & 和 | 除了用于布爾邏輯運(yùn)算外,還可以用于按位運(yùn)算,或者在需要確保兩邊表達(dá)式都被計(jì)算的情況下使用。
- 選擇合適的運(yùn)算符取決于具體的使用場(chǎng)景和需求。在大多數(shù)情況下,使用 && 和 || 可以避免不必要的計(jì)算和潛在的異常,但在需要確保所有表達(dá)式都被計(jì)算的場(chǎng)景中,使用 & 和 | 更為合適。
相關(guān)文章
Spring的@ConfigurationProperties注解詳解
這篇文章主要介紹了Spring的@ConfigurationProperties注解詳解,@ConfigurationProperties該注解是用來(lái)獲取yml或者properties配置文件的配置信息,下面根據(jù)一些配置信息給出案例代碼進(jìn)行講解,需要的朋友可以參考下2023-11-11SpringBoot實(shí)現(xiàn)elasticsearch 查詢操作(RestHighLevelClient 
這篇文章主要給大家介紹了SpringBoot如何實(shí)現(xiàn)elasticsearch 查詢操作,文中有詳細(xì)的代碼示例和操作流程,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07新手小白學(xué)JAVA 日期類Date SimpleDateFormat Calendar(入門)
本文主要介紹了JAVA 日期類Date SimpleDateFormat Calendar,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10java中ImageReader和BufferedImage獲取圖片尺寸實(shí)例
這篇文章主要介紹了java中ImageReader和BufferedImage獲取圖片尺寸實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01java實(shí)現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問題示例
這篇文章主要介紹了java實(shí)現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問題示例,需要的朋友可以參考下2014-05-05完美解決MybatisPlus插件分頁(yè)查詢不起作用總是查詢?nèi)繑?shù)據(jù)問題
這篇文章主要介紹了解決MybatisPlus插件分頁(yè)查詢不起作用總是查詢?nèi)繑?shù)據(jù)問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Java 反射獲取類詳細(xì)信息的常用方法總結(jié)
Java 反射獲取類詳細(xì)信息的常用方法總結(jié),需要的朋友可以參考一下2013-03-03Java泛型在集合使用與自定義及繼承上的體現(xiàn)和通配符的使用
泛型又稱參數(shù)化類型,是Jdk5.0 出現(xiàn)的新特性,解決數(shù)據(jù)類型的安全性問題,在類聲明或?qū)嵗瘯r(shí)只要指定好需要的具體的類型即可。Java泛型可以保證如果程序在編譯時(shí)沒有發(fā)出警告,運(yùn)行時(shí)就不會(huì)產(chǎn)生ClassCastException異常。同時(shí),代碼更加簡(jiǎn)潔、健壯2021-09-09