Java中==和equals的比較

來源:互聯網
上載者:User

經實際驗證,有如下結論:

1. 對於基本類型(只能用==)
==           比較值

2. 對於基本類型的封裝類(Boolean ,Integer,Double,Long,Float)
==           比較地址
equals    比較值

3. 對於類對象
==           比較地址
equals    比較值

4. 對於String
==           比較地址
equals    比較值

驗證用代碼如下:

import java.util.ArrayList;

public class TestEquals {

    public static void main(String[] args) {
 // To validate basic data types
 int intA = 1;
 int intB = 1;
 System.out.println("intA == intB is " + (intA == intB));
 
 // To validate wrapper classes
 Integer integerA = new Integer(1);
 Integer integerB = new Integer(1);
 Integer integerC = integerA;
 System.out.println("------------------------------");
 System.out.println("integerA == integerB is " + (integerA == integerB));
 System.out.println("integerA.equals(integerB) is " + (integerA.equals(integerB)));
 System.out.println("integerA == integerC is " + (integerA == integerC));
 System.out.println("integerA.equals(integerC) is " + (integerA.equals(integerC)));
 
 // To validate ordinary classes
 ArrayList<Integer> arrayListA = new ArrayList<Integer>();
 arrayListA.add(integerA);
 ArrayList<Integer> arrayListB = new ArrayList<Integer>();
 arrayListB.add(integerA);
 ArrayList<Integer> arrayListC = arrayListA;
 System.out.println("------------------------------");
 System.out.println("arrayListA == arrayListB is " + (arrayListA == arrayListB));
 System.out.println("arrayListA.equals(arrayListB) is " + (arrayListA.equals(arrayListB)));
 System.out.println("arrayListA == arrayListC is " + (arrayListA == arrayListC));
 System.out.println("arrayListA.equals(arrayListC) is " + (arrayListA.equals(arrayListC)));

 // To validate String class
 String stringA = new String("123");
 String stringB = new String("123");
 String stringC = stringA;
 System.out.println("------------------------------");
 System.out.println("stringA == stringB is " + (stringA == stringB));
 System.out.println("stringA.equals(stringB) is " + (stringA.equals(stringB)));
 System.out.println("stringA == stringC is " + (stringA == stringC));
 System.out.println("stringA.equals(stringC) is " + (stringA.equals(stringC)));
    }
}

執行結果為:

intA == intB is true
------------------------------
integerA == integerB is false
integerA.equals(integerB) is true
integerA == integerC is true
integerA.equals(integerC) is true
------------------------------
arrayListA == arrayListB is false
arrayListA.equals(arrayListB) is true
arrayListA == arrayListC is true
arrayListA.equals(arrayListC) is true
------------------------------
stringA == stringB is false
stringA.equals(stringB) is true
stringA == stringC is true
stringA.equals(stringC) is true

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.