public class Test{ public static void main(String[] args){ //== compare primitive type System.out.println("compare primitive type data:"); int m=1; int n=1; System.out.println("m==n "+(m==n)); //== compare reference object System.out.println("compare reference object:"); Person a=new Person("jack",12); Person b=new Person("jack",12); System.out.println("a==b "+(a==b)); } } class Person{ String name; int age; public Person(String name,int age){ this.name=name; this.age=age; } }
返回的结果:m==n true
a==b false
==作用于基本数据类型时,则比较其值是否相等;作用于引用数据类型时,则比较两个对象的引用是否相等,即指向同一个对象。
public class Test{ public static void main(String[] args){ //equals compare reference object Person a=new Person("jack",12); Person b=new Person("jack",12); System.out.println("a.equals(b) "+(a.equals(b))); String str1=new String("jack"); String str2=new String("jack"); System.out.println("str1.equals(str2) "+(str1.equals(str2))); } } class Person{ String name; int age; public Person(String name,int age){ this.name=name; this.age=age; } }
返回的结果: a.equals(b) false
str1.equals(str2) true
equals方法是基类Object中的方法,所有继承Object的类都会有该方法。equals方法不能作用于基本数据类型变量。
引用数据类型的两个例子比较的结果不一样。原因是String类重写了equals方法。
Object类中的equals方法
public boolean equals(Object obj){ return (this==obj); }
而String类的equals方法
public boolean equals(Object anObject){ if(this==anObject){ return true; } if(anObject instanceof String){ String anotherString=(String)anObject; int n=value.length; if(n==anotherString.value.length){ char v1[]=value; char v2[]=anotherString.value; int i=0; while(n--!=0){ if(v1[i]!=v2[i]) return false; i++; } return true; } } return false; }
没有重写equals方法的Person类,比较的是对象的引用是否相等;而重写equals方法的String类,则比较的是对象的内容是否相同。
总结:
1)对于==,如果作用于基本数据类型的变量,则直接比较其存储的值是否相等;如果作用于引用数据类型,则比较的是对象引用是否相等,即指向同一对象。
2)对于equals方法,该方法不能作用于基本数据类型的变量。如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;而像String,Date等类对equals方法进行了重写的情况,则比较的是所指向的对象的内容是否相同。
Long对象中的equals方法
public boolean equals(Object obj) { if (obj instanceof Long) { return value == ((Long)obj).longValue(); } return false; }
1,int与int比较,只能==,不能equals。而比较的值相等则返回true,反之返回false。
int i=1000; int j=1000; System.out.println(i==j); //true
2,Integer与int比较。==:Integer自动拆箱,调用intValue()方法,比较两个数的值是否相等;而equals则比较的是两个引用类型的值是否相等。
Integer a=1000; int b=1000; System.out.println(a==b); //true System.out.println(a.equals(b)); //true
3,Integer与Integer比较。==:两个值一样,且都在[-128,127]范围,那么结果为true;两个值一样,但在那个范围之外,结果为false。equals:比较的是两个Integer的值是否一样。
Integer m=-128; Integer n=-128; System.out.println(m==n); //true System.out.println(m.equals(n)); //true
4,new Integer与int比较,类似于Integer与int的比较。
Integer x=new Integer(1000); int y=1000; System.out.println(x==y); //true System.out.println(x.equals(y)); //true
5,new Integer与Integer比较,==:永远是false,因为两个都是不同的对象;equals:当两个值一样时,equals的结果为true。
Integer c=new Integer(10); Integer d=10; System.out.println(c==d); //false System.out.println(c.equals(d)); //true
6,new Integer与new Integer比较,类似于new Integer与Integer的比较。
Integer e=new Integer(100); Integer f=new Integer(100); System.out.println(e==f); //false System.out.println(e.equals(f)); //true
posted on
浙公网安备 33010602011771号