compareTo和equals有什么区别??帮忙,感谢

解决方案 »

  1.   

    compareTo(Object obj) returns  negative, 0(if equals), positive int value. Obj must be of string type, otherwise throw class cast exception.equals(Object obj) returns true/false, if the object is not of string type, returns false.The compareTo method is more often used in sort/search.
      

  2.   

    System.out.println("b".compareTo((Object)"b")); //0
      System.out.println("b".compareTo((Object)"a")); //1 
      System.out.println("b".compareTo((Object)"c")); //-1 
      System.out.println("b".compareTo(new StringBuffer("c"))); // exception
      System.out.println("b".equals(new StringBuffer("b"))+"  "+"b".equals("b")); // false true
      

  3.   

    compareTo(Object o) calls compareTo(String s).
      

  4.   

    equals只能比较两个String是否相等,compareTo可以比较两个String的大小.
    例如:
    String beginDate = "20051001";
    String endDate = "20051012";beginDate.equals(endDate)结果为false;
    beginDate.compareTo(endDate)返回的是第一个不相等字符的编码的差