Java中两个对象的比较 equals()方法和==号的区别

在Java中,时不时我们会把两个对象进行,然而得到的结果却不是我们想的结果,这是怎么回事呢?


一、两种形式的比较:比较时,我们要弄清楚是哪一种比较。   


 1.值类型比较     

即内容相同,我们就认为是相等的。比如:int i=5;int j =5;此时我们说i和j相等,其实指的是i和j的内容相同。  

   

2.引用类型比较    

但在Java中,除了值类型,另外还有一种引用类型,而不同的对象,其引用值其实并不相等,即在内存中的不同的地 址单元中。比如我们定义了学生类,分别有两个学生对象实例 :   

      Student stu= new Student();   Student stu1= new Student();


 此时我们无论是使用stu==stu1符号,或者stu.equals(stu1)方法,把两个对象进行比较,得到的结果都是不相等的,因为对于引用类型来说,默认是比较两个对象引用的地址,显示,每个对象的引用有自己唯一的地址,所以,是不相等。


二、有时,我们比较两个对象时,如果他们的内容一样,那么我们就认为这两个对象是相等的,如上面的两个学生对象。这时,我们该怎么办呢?其实,非常简单,只要在类里面重写equals()方法,进行对象里面的内容比较久可以了。如上面,我们只需要在Student类中重写equals()方法即可。    


下面,让我们来看看实例吧!     没有重写equals()方法时的比较:


学生类:Student类

[java]  view plain  copy
  1. package com.bluesky;  
  2.   
  3. public class Student {  
  4.       
  5.     String name;  
  6.       
  7.     public Student(){  
  8.           
  9.     }  
  10.       
  11.     public Student(String name){  
  12.           
  13.         this.name=name;  
  14.           
  15.     }</strong>  
  16.       
  17.   
  18. }</span>  

测试类Test:

[java]  view plain  copy
  1. package com.bluesky;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void main(String[] args) {  
  6.   
  7.       int i=5;  
  8.       int j=5;  
  9.         
  10.       if(i==j) System.out.println("i和j相等!");  
  11.       else System.out.println("不相等!");  
  12.         
  13.       Student s = new Student("BlueSky");  
  14.       Student s1=new Student("BlueSky");  
  15.         
  16.       if(s==s1)  System.out.println("s和是s1相等!");  
  17.       else System.out.println("s和是s1不相等!");  
  18.         
  19.       if(s.equals(s1)) System.out.println("s和是s1相等!");  
  20.       else System.out.println("s和是s1不相等!");  
  21.     }  
  22. }  

运行结果:

重写equals()方法后再次进行比较:

 Student类:

[java]  view plain  copy
  1. package com.bluesky;  
  2.   
  3. public class Student {  
  4.       
  5.     String name;  
  6.       
  7.     public Student(){  
  8.           
  9.     }  
  10.       
  11.     public Student(String name){  
  12.           
  13.         this.name=name;  
  14.           
  15.     }  
  16.   
  17.       
  18.     public boolean equals(Object obj) {  
  19.         if (this == obj)      //传入的对象就是它自己,如s.equals(s);肯定是相等的;  
  20.             return true;   
  21.         if (obj == null)     //如果传入的对象是空,肯定不相等  
  22.             return false;  
  23.         if (getClass() != obj.getClass())  //如果不是同一个类型的,如Studnet类和Animal类,  
  24.                                            //也不用比较了,肯定是不相等的  
  25.             return false;  
  26.         Student other = (Student) obj;       
  27.         if (name == null) {  
  28.             if (other.name != null)  
  29.                 return false;  
  30.         } else if (!name.equals(other.name))   //如果name属性相等,则相等  
  31.             return false;  
  32.         return true;  
  33.     }  
  34.       
  35.   
  36. }  

测试类Test:

[java]  view plain  copy
  1. package com.bluesky;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void main(String[] args) {  
  6.   
  7.       int i=5;  
  8.       int j=5;  
  9.         
  10.       if(i==j) System.out.println("i和j相等!");  
  11.       else System.out.println("不相等!");  
  12.         
  13.       Student s = new Student("BlueSky");  
  14.       Student s1=new Student("BlueSky");  
  15.         
  16.       if(s==s1)  System.out.println("s和是s1相等!");  
  17.       else System.out.println("s和是s1不相等!");  
  18.         
  19.       if(s.equals(s1)) System.out.println("s和是s1相等!");  
  20.       else System.out.println("s和是s1不相等!");  
  21.     }  
  22. }  


运行结果:



重写equals()方法后,得到s和s1相等。==对引用类型的只能进行地址比较,故还是不相等的。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值