equals使用总结
一、equals和==
1.equals和==的区别:前者比较内容,后者比较地址。
2.equals比较内容,但是需要覆盖这个方法,设定比较规则,JDK有一些类默认已经覆盖过了oject类的equals()方法,这些类有:java.io.file,java.util.Date,java.lang.string,包装类(Integer,Double等)
3."a"和"a "用equals比较是false,也就是说有没有空格,内容是不同的(地址也不同)。
二、常量池
Java为了提高性能,为八种基本类型和String类提供了对象池机制,当然Java的八种基本类型的包装类(Packaging Type)也有对象池机制。
public static void main(String[] args) {Integer i1 = new Integer(100);Integer i2 = new Integer(100);System.out.println(i1 == i2);System.out.println(i1.equals(i2));System.out.println("========");Integer i3 = 127;//128Integer i4 = 127;System.out.println(i3 == i4);System.out.println(i3.equals(i4));System.out.println("========");Integer i5 = 500;Integer i6 = 500;System.out.println(i5 == i6);System.out.println(i5.equals(i6));System.out.println("========");String strA1 = "test";String strB1 = "test";String strC1 = "test ";System.out.println(strA1 == strB1);System.out.println(strA1 == strC1);}结果:
falsetrue========truetrue========falsetrue========truefalse
结论:
//通过i3==i4为ture,i5==i6为flase可知有的整数被放进“常量池”,有的没有,127是临界点
//通过strA1 == strB1为ture可知,字符串常量,是被放进常量池的
关于数值计算:
public static void objPoolTest() {Integer i1 = 40;Integer i2 = 40;Integer i3 = 0;Integer i4 = new Integer(40);Integer i5 = new Integer(40);Integer i6 = new Integer(0);Integer i7=i5 + i6;System.out.println("i1=i2\t" + (i1 == i2));System.out.println("i1=i2+i3\t" + (i1 == i2 + i3));System.out.println("i4=i5\t" + (i4 == i5));System.out.println("i4=i5+i6\t" + (i4 == i5 + i6));System.out.println("i4=i7\t" + (i4 == i7));System.out.println();}结果:
i1=i2truei1=i2+i3truei4=i5falsei4=i5+i6truei4=i7false
结论:
对于i1==i2+i3、i4==i5+i6结果为True,是因为,Java的数学计算是在内存栈里操作的,Java会对i5、i6进行拆箱操作,其实比较的是基本类型(40=40+0),他们的值相同,因此结果为True。但是i4和i7地址不同,因为i7是提前算好的,所以i4和i7不属于内在中比较。
关于字符串计算:
public static void strPoolTest() {String str1 = "ab";String str2 = "ab";String str3 = "a";String str4 = "b";String str5 = new String("ab");String str6 = new String("ab");String str7 = new String("a");String str8 = new String("b");String str9=str7 + str8;System.out.println("str1=str2\t" + (str1 == str2));System.out.println("str1=str3+str4\t" + (str1 == str3 + str4));System.out.println("str5=str6\t" + (str5 == str6));System.out.println("str5=str7+str8\t" + (str5 == str7 + str8));System.out.println("str5=str9\t" + (str5 == str9));System.out.println();}结果:
str1=str2truestr1=str3+str4falsestr5=str6falsestr5=str7+str8falsestr5=str9false
结论:
字符串的计算和数值是不一样的。
三、String和StringBuffer的equals
public static void main(String[] args) {System.out.println(new StringBuffer().toString().equals(new StringBuffer("").toString()));System.out.println(new StringBuffer().equals(new StringBuffer("")));System.out.println(new StringBuffer().toString().equals(new StringBuffer("")));System.out.println(new StringBuffer().toString().contentEquals(new StringBuffer("")));}结果:
truefalsefalsetrue
结论:
//结论:第一个为true,说明String类重写过了 equals方法
//结论:第二个为false,说明StringBuffer类没有重写过了 equals方法
//结论:第三个为false,因为String 与 StringBuffer不是同一类型
//结论:第四个为true,说明要判断String,StringBuffer,StringBuilder的内容是否相等,用contentEquals(CharSequence cs)
// 另:String 类中 equals方法的说明
//public boolean equals(Object anObject)将此字符串与指定的对象比较。
//当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。
// String 类中 contentEquals方法的说明
//public boolean contentEquals(CharSequence cs)将此字符串与指定的 CharSequence 比较。
//当且仅当此 String 与指定序列表示相同的 char 值序列时,结果才为 true。
// CharSequence 所有已知实现类:
//CharBuffer, Segment, String, StringBuffer, StringBuilder