java對象比較==和equals的區別


轉載:http://blog.csdn.net/bluesky_usc/article/details/51849125

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類

 1 class Student
 2 {
 3     int num;
 4     String name;
 5     Student(int num,String name){
 6         this.num=num;
 7         this.name=name;
 8     }
 9     
10     public int hashCode(){
11         return num*name.hashCode();
12     }
13     
14     public boolean equals(Object obj){
15         Student s =(Student)obj;
16         return num==s.num && name.equals(s.name);
17     }
18     
19     public String toString(){
20         return num+"name:"+name;
21     }
22 }
View Code

 

 

測試類Test:

 

 

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類:

package com.bluesky;  


1.  
2.public class Student {  
3.      
4.    String name;  
5.      
6.    public Student(){  
7.          
8.    }  
9.      
10.    public Student(String name){  
11.          
12.        this.name=name;  
13.          
14.    }  
15.  
16.      
17.    public boolean equals(Object obj) {  
18.        if (this == obj)      //傳入的對象就是它自己,如s.equals(s);肯定是相等的;  
19.            return true;   
20.        if (obj == null)     //如果傳入的對象是空,肯定不相等  
21.            return false;  
22.        if (getClass() != obj.getClass())  //如果不是同一個類型的,如Studnet類和Animal類,  
23.                                           //也不用比較了,肯定是不相等的  
24.            return false;  
25.        Student other = (Student) obj;       
26.        if (name == null) {  
27.            if (other.name != null)  
28.                return false;  
29.        } else if (!name.equals(other.name))   //如果name屬性相等,則相等  
30.            return false;  
31.        return true;  
32.    }  
33.      
34.  
35.}  

 

測試類Test:

 package com.bluesky;  

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

運行結果:

 

 

重寫equals()方法后,得到s和s1相等。==對引用類型的只能進行地址比較,故還是不相等的。

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM