數(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;
}
}
最后編輯于 :2018.03.18 00:36:00
?著作權(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ù)。