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