<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java 程序:比較字符串 > 原文: [https://www.programiz.com/java-programming/examples/compare-strings](https://www.programiz.com/java-programming/examples/compare-strings) #### 在此程序中,您將學習比較 Java 中的兩個字符串。 ## 示例 1:比較兩個字符串 ```java public class CompareStrings { public static void main(String[] args) { String style = "Bold"; String style2 = "Bold"; if(style == style2) System.out.println("Equal"); else System.out.println("Not Equal"); } } ``` 運行該程序時,輸出為: ```java Equal ``` 在上面的程序中,我們有兩個字符串`style`和`style2`。 我們僅使用相等運算符(`==`)比較兩個字符串,這會將值`Bold`與`Bold`進行比較,并輸出`Equal`。 * * * ## 示例 2:使用`equals()`比較兩個字符串 ```java public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); if(style.equals(style2)) System.out.println("Equal"); else System.out.println("Not Equal"); } } ``` 運行該程序時,輸出為: ```java Equal ``` 在上面的程序中,我們有兩個字符串`style`和`style2`都包含相同的全局`Bold`。 但是,我們使用了`String`構造器來創建字符串。 要在 Java 中比較這些字符串,我們需要使用字符串的`equals()`方法。 您不應該使用`==`(相等運算符)來比較這些字符串,因為它們會比較字符串的引用,即它們是否是同一對象。 另一方面,`equals()`方法比較字符串的值是否相等,而不是對象本身。 如果改為將程序更改為使用等號運算符,則會顯示`Not Equal`,如下所示。 * * * ## 示例 3:使用`==`比較兩個字符串對象(不起作用) ```java public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); if(style == style2) System.out.println("Equal"); else System.out.println("Not Equal"); } } ``` 運行該程序時,輸出為: ```java Not Equal ``` * * * ## 示例 4:比較兩個字符串的不同方法 這是在 Java 中可能進行的字符串比較。 ```java public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); boolean result = style.equals("Bold"); // true System.out.println(result); result = style2 == "Bold"; // false System.out.println(result); result = style == style2; // false System.out.println(result); result = "Bold" == "Bold"; // true System.out.println(result); } } ``` 運行該程序時,輸出為: ```java true false false true ```
                  <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>

                              哎呀哎呀视频在线观看