<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java `Optional`示例 > 原文: [https://javatutorial.net/java-optional-example](https://javatutorial.net/java-optional-example) Java 8 引入了`Optional`類,該類用于根據值是否存在來操縱數據。 您可以不使用此類而具有相同的功能,但是最終會產生混亂的代碼。 換句話說,您的空檢查將更少,而`NullPointerException`則不會。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) 讓我舉一個例子,說明為什么不使用`Optional`會導致問題。 ## 未使用`Optional` ```java public class NonOptionalImplementation { public static void main(String[] args) { Animal dog = new Animal(null, 5); String dogModified = dog.getName().toLowerCase(); System.out.println(dogModified); } } class Animal { private String name; private int age; Animal(String n, int a) { this.name = n; this.age = a; } public String getName() { return this.name; } public int getAge() { return this.age; } } ``` 您能發現哪里會出問題嗎? 顯然,當我們到達`print`語句時,由于對象名稱被故意設置為`null`,它將拋出`NullPointerException`。 ## 輸出 ```java Exception in thread "main" java.lang.NullPointerException at OptionalImplementation.main(OptionalImplementation.java:6) ``` ## 使用`Optional`解決了此問題 ```java import java.util.Optional; import java.util.*; public class OptionalImplementation { public static void main(String[] args) { Animal dog = new Animal(null, 5); System.out.println(Optional.ofNullable(dog.getName()).orElse("Name not provided")); } } class Animal { private String name; private int age; Animal(String n, int a) { this.name = n; this.age = a; } public String getName() { return name; } public int getAge() { return this.age; } } ``` ## 輸出 ```java Name not provided ``` ## 上述實現的簡要拆分 * 我們正在創建一個非常簡單的類,稱為`Animal`,我們有 2 個具有構造函數的 getter 方法 * 在我們的主方法中,我們正在創建該`Animal`類的實例,但是我們還將第一個參數(恰好是名稱)分配給`null` * `Optional.ofNullable`檢查傳遞的字符串是否為`null` * 之后,我們嘗試打印值 * 我們有這個`.orElse()`方法調用,這基本上意味著:如果您要在其上調用該方法的字符串為 null,那么我將使用給出的參數進行打印 * 如果不為空,那么我將打印原始字符串 * 換句話說,如果字符串為`null`,它將打印默認值 是不是很酷? 只要看看我們檢查空值時代碼的外觀如何優雅即可。 而且安全! 為了進行比較,讓我們嘗試不使用`Optional`類而獲得相同的結果。 ```java public class OptionalImplementation { public static void main(String[] args) { Animal dog = new Animal(null, 5); if (dog.getName() == null) { System.out.println("Name not provided"); } else { System.out.println(dog.getName().toLowerCase()); } } } class Animal { private String name; private int age; Animal(String n, int a) { this.name = n; this.age = a; } public String getName() { return this.name; } public int getAge() { return this.age; } } ``` ## 輸出 ```java Name not provided ``` 它有效..但是干凈嗎? 我不這么認為! 您也可以使用`isPresent()`方法檢查對象中是否存在值: ```java import java.util.Optional; public class OptionalImplementation { public static void main(String[] args) { Optional<Object> n = Optional.empty(); // creating an empty string System.out.println(n.isPresent()); System.out.println(n); } } ``` ## 輸出 ```java false Optional.empty ``` 而已。 不是那么難,不是嗎? 甚至它都不是很容易理解,但是它也大大簡化了您的代碼,并且也非常易于閱讀和維護。 如果要瀏覽`Optional`提供的所有方法,請查看官方 [Oracle 文檔](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看