compareTo 在 Java 中有什么用


Java 中的 compareTo() 方法按字母顺序比较两个字符串。

示例

实时演示

public class Test {
   public static void main(String args[]) {
      String str1 = "Strings are immutable";
      String str2 = new String("Strings are immutable");
      String str3 = new String("Integers are not immutable");

      int result = str1.compareTo( str2 );
      System.out.println(result);

      result = str2.compareTo( str3 );
      System.out.println(result);
   }
}

输出

0
10

更新于:2020 年 2 月 26 日

129 次浏览

点亮您的 事业

完成课程获取认证

开始
广告