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.
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
例如:
String beginDate = "20051001";
String endDate = "20051012";beginDate.equals(endDate)结果为false;
beginDate.compareTo(endDate)返回的是第一个不相等字符的编码的差