Java常量池、==、equals用法剖析

數(shù)據(jù)存儲(chǔ)類型
存儲(chǔ)器類型 存儲(chǔ)區(qū)域
內(nèi)存 RAM、ROM
外存 硬盤(pán)

RAM:random access memory,隨機(jī)存儲(chǔ)器;可讀可寫(xiě);不存儲(chǔ);運(yùn)行內(nèi)存
ROM:read only memory,只讀存儲(chǔ)器;只可讀;存儲(chǔ)

數(shù)據(jù)存儲(chǔ)區(qū)域
存儲(chǔ)區(qū)名稱 存儲(chǔ)數(shù)據(jù)類型 存儲(chǔ)區(qū)域 功能特性
寄存器 處理器內(nèi)部 最快存儲(chǔ)區(qū)
對(duì)象引用、基本數(shù)據(jù)類型值 通用RAM 堆棧指針向下移動(dòng),分配新內(nèi)存;指針向上移動(dòng),釋放舊內(nèi)存;數(shù)學(xué)計(jì)算在內(nèi)存棧進(jìn)行
所有Java對(duì)象、new創(chuàng)建新對(duì)象、構(gòu)造器創(chuàng)建對(duì)象 通用內(nèi)存池(位于RAM區(qū)) 自動(dòng)分配內(nèi)存,無(wú)序
常量存儲(chǔ)(常量池、靜態(tài)區(qū)) 程序中的字面量(Java變量、常量值) 程序代碼內(nèi)部、ROM區(qū) 不可變
非RAM存儲(chǔ) 流對(duì)象、持久化對(duì)象等 磁盤(pán)、硬盤(pán)、數(shù)據(jù)庫(kù)等 獨(dú)立存在

常量池中包含:
1.代碼中所定義的各種基本數(shù)據(jù)類型
2.對(duì)象型(String和數(shù)組)的常量值
3.以文本形式出現(xiàn)的符號(hào)引用:方法、字段、類和接口的名稱和描述符

名稱 常量池
實(shí)現(xiàn)類 Byte、Short、Integer、Long、Character、Boolean、String
未實(shí)現(xiàn)類 Float、Double
存儲(chǔ)范圍 -128~127(超出部分存入Heap堆)

實(shí)戰(zhàn)1:==與equals

數(shù)據(jù)類型 == equals
基本數(shù)據(jù)類型 比較數(shù)值 --
包裝類、String類 比較地址 比較內(nèi)容(重寫(xiě)equals方法)

注意:Integer類、String類對(duì)equals方法重寫(xiě),但父類Object類未重寫(xiě)equals方法,故其他子類equals方法比較的也是“引用”地址。

類型1:a與b(Integer類、Double類)
類型 a b == equals 解析
1 int a=89; Integer b=89; true -- --
2 int a=129 Integer b=129; true -- --
3 int a=89; Integer b=new Integer(89); true -- --
4 int a=129; Integer b=new Integer(129); true -- --
5 Integer a=89; Integer b=89; true true -128~127
6 Integer a=129; Integer b=129; false true -128~127
7 Integer a=89; Integer b=new Integer(89); false true new
8 Integer a=129; Integer b=new Integer(129); false true new
9 double a=129.1 double b=129.1; true -- --
10 Double a=129.1 Double b=129.1; false true Double不支持常量池
類型2:a+b與c(Integer類)
類型 a b c a+b==c equals
1 Integer a=129; Integer b=0; Integer c=129; true true
2 Integer a=new Integer(129); Integer b=0; Integer c=new Integer(129); true true

解析:Integer對(duì)象無(wú)法與數(shù)值進(jìn)行直接比較,所以Integer對(duì)象首先進(jìn)行拆箱后進(jìn)行數(shù)值計(jì)算。(參考資料:技術(shù)小黑屋-Java中的自動(dòng)裝箱與拆箱

類型3:a+b與c(String類)
類型 操作 ==
1 String a="hello";String b="world";String c="helloworld"; a+b==c false
2 String a="hello";String b="world";String c="helloworld"; a+"world"==c false
3 String a="hello"+"world";String c="helloworld"; a==c true
4 final String a="hello";final String b="world";String c="helloworld"; a+b==c true
5 final String a="hello";final String b="world";String c="helloworld"; a+"world"==c true

解析:String類的intern()方法檢測(cè)常量池中是否存在字符串,若存在返回引用,若不存在,則創(chuàng)建并返回引用。(同類參考:夢(mèng)工廠-Java常量池理解與總結(jié)

實(shí)戰(zhàn)2:重寫(xiě)equals()方法和hashCode()方法
public class Main {
    private int num;
    private String name;
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;`
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Main main = (Main) obj;
        if (name == null) {
            if (main.name != null) {
                return false;
            }
        } else if (!name.equals(main.name)) {
            return false;
        }
        return true;
    }
    @Override
    public int hashCode(){
        final int prime=31;
        int hash=1;
        hash=prime*hash+num;
        hash=prime*hash+((name==null)?0:name.hashCode());
        return hash;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一.相關(guān)概念 什么是常量用final修飾的成員變量表示常量,值一旦給定就無(wú)法改變!final修飾的變量有三種:靜態(tài)...
    夢(mèng)工廠閱讀 58,619評(píng)論 38 275
  • 相關(guān)概念 常量池的定義常量池(constant pool):指的是在編譯期被確定,并被保存在已編譯的.class文...
    snoweek閱讀 852評(píng)論 0 4
  • 結(jié)果: 結(jié)果分析: 1.i和i0均是普通類型(int)的變量,所以數(shù)據(jù)直接存儲(chǔ)在棧中,而棧有一個(gè)很重要的特性:棧中...
    撐起頭頂?shù)奶?/span>閱讀 384評(píng)論 0 0
  • 學(xué)習(xí)和使用java已經(jīng)有4年多了,反省一下,太多關(guān)注了實(shí)際的應(yīng)用層面,對(duì)某些基礎(chǔ)的東西沒(méi)有特別理解透徹。好記性不如...
    錦書(shū)詩(shī)詞閱讀 1,732評(píng)論 4 26
  • 這是一個(gè)超逗男同學(xué)和我的聊天記錄 啥也不說(shuō)了 放圖 你的支持是我的動(dòng)力?..歡迎關(guān)注微信公眾號(hào):搞笑有嘻哈
    搞笑有嘻哈閱讀 750評(píng)論 0 0

友情鏈接更多精彩內(nèi)容