轉載自 這裡
前言 :
在Java裡,equals和==常令程式設計師困惑,這裡簡單說明如下 :
1. ==
2. equals
範例程式 :
下面以簡單的範例程式說明 equals 與 == 的差別 :
執行結果 :
補充說明 :
前言 :
在Java裡,equals和==常令程式設計師困惑,這裡簡單說明如下 :
1. ==
測試物件參考時,唯有當兩個參考指向同一物件時,==運算子的結果才為true,同樣的,唯有當兩個參考指向不同物件時,!=運算子的結果才為true,這兩個運算子的處理與物件的內容無關。需要特別注意的是,在String中使用==,因為Java為節省記憶體,會在某一輪調區中維護唯一的String物件,所以如果在類別裡使用同一字串,Java只會建立一個唯一的字串而已。
2. equals
另一種測試的做法是equals,equals在類別為Java原先就存在時,是比較兩個物件是否為相同類型的類別後再比較其內容值是否相同,是就傳回true,否就傳回false。當要測試的類別是自定類別時,要依自定類別所提供的equals來決定如何比較,如果自定類別沒有覆蓋Object的equals類別的話,就以Object的equals來比較,Object的equals的比較方式如同==。
範例程式 :
下面以簡單的範例程式說明 equals 與 == 的差別 :
- myObject1.java 代碼 :
- package test;
- public class myObject1 {
- int nNum = 100;
- public void setNum(int nN) {
- nNum = nN;
- }
- public int getNum() {
- return nNum;
- }
- public void printNum() {
- System.out.println(nNum);
- }
- }
- myObject2.java 代碼 :
- package test;
- public class myObject2 {
- int nNum = 100;
- public void setNum(int nN) {
- nNum = nN;
- }
- public int getNum() {
- return nNum;
- }
- public void printNum() {
- System.out.println(nNum);
- }
- public boolean equals(Object obj) {
- if (obj == null) return false;
- if (!(obj instanceof myObject2)) return false;
- return this.nNum == ((myObject2) obj).getNum();
- }
- }
- myEquals.java 代碼 :
- package test;
- public class myEquals {
- public myEquals() {
- //字串型別
- String sA = "ABC";
- String sB = "ABC";
- String sC = new String("ABC");
- //==的比較
- if (sA == sB)
- System.out.println("sA == sB (Because of String pool)");
- else
- System.out.println("sA != sB");
- if (sA == sC)
- System.out.println("sA == sC");
- else
- System.out.println("sA != sC (Because sC uses new operator)");
- //equals的比較
- if (sA.equals(sB))
- System.out.println("sA.equals(sB) = true (Because of the same value)");
- else
- System.out.println("sA.equals(sB) = false");
- if (sA.equals(sC))
- System.out.println("sA.equals(sC) = true (Because of the same value)");
- else
- System.out.println("sA.equals(sC) = false");
- //Java原有的型別
- Long lngA = new Long(100);
- Long lngB = new Long(100);
- Long lngC = new Long("100");
- //==的比較
- if (lngA == lngB)
- System.out.println("lngA == lngB");
- else
- System.out.println("lngA != lngB (Because of the new operator)");
- if (lngA == lngC)
- System.out.println("lngA == lngC");
- else
- System.out.println("lngA != lngC (Because of the new operator)");
- //equals的比較
- if (lngA.equals(lngB))
- System.out.println("lngA.equals(lngB) = true (Same value)");
- else
- System.out.println("lngA.equals(lngB) = false");
- if (lngA.equals(lngC))
- System.out.println("lngA.equals(lngC) = true (Same value)");
- else
- System.out.println("lngA.equals(lngC) = false");
- //自定型別
- myObject1 m1A = new myObject1();
- myObject1 m1B = new myObject1();
- myObject2 m2A = new myObject2();
- myObject2 m2B = new myObject2();
- //==的比較
- if (m1A == m1B)
- System.out.println("m1A == m1B");
- else
- System.out.println("m1A != m1B (Not same memory address)");
- if (m2A == m2B)
- System.out.println("m2A == m2B");
- else
- System.out.println("m2A != m2B (Not same memory address)");
- //equals的比較
- if (m1A.equals(m1B))
- System.out.println("m1A.equals(m1B) = true");
- else
- System.out.println("m1A.equals(m1B) = false (Not overwtire equals method)");
- if (m2A.equals(m2B))
- System.out.println("m2A.equals(m2B) = true (Rewrite equals method)");
- else
- System.out.println("m2A.equals(m2B) = false");
- }
- public static void main(String[] args) {
- myEquals myEquals1 = new myEquals();
- }
- }
執行結果 :
sA == sB (Because of String pool)
sA != sC (Because sC uses new operator)
sA.equals(sB) = true (Because of the same value)
sA.equals(sC) = true (Because of the same value)
lngA != lngB (Because of the new operator)
lngA != lngC (Because of the new operator)
lngA.equals(lngB) = true (Same value)
lngA.equals(lngC) = true (Same value)
m1A != m1B (Not same memory address)
m2A != m2B (Not same memory address)
m1A.equals(m1B) = false (Not overwtire equals method)
m2A.equals(m2B) = true (Rewrite equals method)
補充說明 :
This message was edited 2 times. Last update was at 03/12/2010 10:34:56
有一段看不懂,想請教前輩:
回覆刪除//自定型別
myObject1 m1A = new myObject1();
myObject1 m1B = new myObject1();
myObject2 m2A = new myObject2();
myObject2 m2B = new myObject2();
//equals的比較
if (m1A.equals(m1B))
System.out.println("m1A.equals(m1B) = true");
else
System.out.println("m1A.equals(m1B) = false (Not overwtire equals method)");
if (m2A.equals(m2B))
System.out.println("m2A.equals(m2B) = true (Rewrite equals method)");
else
System.out.println("m2A.equals(m2B) = false");
這段的結果:
m1A.equals(m1B) = false (Not overwtire equals method)
m2A.equals(m2B) = true (Rewrite equals method)
看不懂m1A、m1B、m2A、m2B的差異,可以請你詳細說明一下嗎?
m1X (m1A, m1B) 類別並沒有改寫 equals(), 也就是使用預設 Object 的 equals(): 判斷是否為同一個物件.
刪除m2X (m2A, m2B) 類別改寫了 equals():
1. if (obj == null) return false; 首先判斷輸入物件如果是 null 則返回 false
2. if (!(obj instanceof myObject2)) return false; 接著判斷輸入物件是否是此物件類別延伸出來的物件, 不是就返回 false
3. return this.nNum == ((myObject2) obj).getNum(); 最後判斷兩個物件的 field (nNum) 是否相等, 是就返回 true, 否則 false
也就是說 m1A 的 equals() 是 Object 來的, 判斷兩個物件是否為同一個.
因為 m1A 跟 m1B 不是同一個物件, 故 m1A.equals(m2B)=false
而 m2A 與 m2B 都是 class myObject2 並改寫了 equals(). 故:
1. m2B 不是 null -> 2. m2B 與 m2A 都是 class myObject2 -> 3. m2B 與 m2A 的 field (nNum) 相等.
因此 m2A.equals(m2B) = true
Although an older version of Quick Luck, it continues to display high effectiveness in beating drug tests. Here is what you will find in the Sub Solution Synthetic Urine Kit: • 88 ml container • A vial of powdered urine • Temperature strip • 1 heat activator powder Pros: • One of the most reliable synthetic urine kits with a 17-year long track record • Undetectable biocide-free and toxins-free formula • It contains 13 chemical compounds with just the right amount of urea and uric acid • pH levels and specific gravity fall under the normal range • Heat activator powder provides a less-time consuming way to heat synthetic urine • Separate mixing container and urine vial are given to avoid cross-contamination Cons: • It does not come pre-mixed • There is no heating pad included in case the heat activator fails • Some may find the price to be a tad bit high Features: Unlike Quick Luck, Sub Solution is Clear Choice's powdered urine that needs to be mixed for use. It is an older product, but its concentration of urea, uric acid, and creatinine are on point. It contains all the 13 different chemicals that naturally occur in real human urine for normal pH levels and specific gravity. The Urinator is an electronic urine tester that uses only one pair of batteries to keep the temperature stable for at least four hours. The Urinator is a dependable and reusable device. Visit: https://www.urineworld.com/
回覆刪除