读书人

String比较步骤结果

发布时间: 2012-12-19 14:13:14 作者: rapoo

String比较方法结果
A= "abcd "
B= "abcd "

A==B 是返回 true. 因为A,B都一样.

A=new String( "abcd ")
B=new String( "abcd ")

A==B 则返回 flase
而 A.equals(B) 输出为 true


String s1 = "abc";
String s2 = "cd";
int r = s2.compareTo(s1);
System.out.println("r = " + r);
输出为 r = 2
int r = s1.compareTo(s2);
输出为 r = -2

String s1 = "abc";
String s2 = "bc";
int r = s2.compareTo(s1);
输出为 r = 1
int r = s1.compareTo(s2);
输出为 r = -1

String s1 = "abc";
String s2 = "abc";
int r = s2.compareTo(s1);
输出为 r = 0

String s1 = "abc";
String s2 = "ab";
int r = s2.compareTo(s1);
输出为 r = -1
int r = s1.compareTo(s2);
输出为 r = 1

String s1 = "abc";
String s2 = "gab";
int r = s1.compareTo(s2);
输出为 r = -6

String s1 = "abc";
String s2 = "fabc";
int r = s1.compareTo(s2);
输出为 r = -5

相关链接:http://leepoint.net/notes-java/data/expressions/22compareobjects.html

读书人网 >编程

热点推荐