Java的封裝類和裝箱拆箱詳解
一、封裝類
1.封裝類概念
Java中存在基礎(chǔ)數(shù)據(jù)類型,但是在某些情況下,我們要對基礎(chǔ)數(shù)據(jù)類型進行對象的操作,例如,集合中只能存對象,而不能存在基礎(chǔ)數(shù)據(jù)類型,于是便出現(xiàn)了封裝類。封裝類就是對基本數(shù)據(jù)類型進行封裝,并用它生成對象,以便以對象方式操作基本數(shù)據(jù)類型。每一個基本數(shù)據(jù)類型都對應(yīng)一種封裝類。
2. 各個基礎(chǔ)類型對應(yīng)的封裝類
| 基礎(chǔ)類型 | 封裝類型 |
|---|---|
| int | Integer |
| byte | Byte |
| short | Short |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
| char | Character |
二、裝箱與拆箱
1.裝箱與拆箱概念
- 裝箱:將基礎(chǔ)數(shù)據(jù)類型自動轉(zhuǎn)化為對應(yīng)的封裝類
- 拆箱:將封裝類自動轉(zhuǎn)化為對應(yīng)的基礎(chǔ)數(shù)據(jù)類型
2.基礎(chǔ)數(shù)據(jù)類型封裝
public class Test {
public static void main(String[] args) {
int num = 1;
Object obj = new Num(num);//父類型引用指向子類型對象
System.out.println(obj);
}
}public class Num {
int num;
public Num(int num) {
this.num = num;
}
public String toString() {
return "" + num;
}
}
//實現(xiàn)封裝3.自動裝箱拆箱演示
public class Test {
public static void main(String[] args) {
int num = 10;
Integer num1 = num; // 自動裝箱
int num2 = num1; // 自動拆箱
}
}上面代碼中,首先,num自動裝箱為Integer類對象賦值給num1;然后,num1又自動拆箱為基本數(shù)據(jù)類型。
4.Integer中valueOf方法和 intValue方法源碼
Integer在裝箱過程中調(diào)用了Integer中的valueOf方法,拆箱時調(diào)用了Integer中的intValue方法。
valueOf方法:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}intValue方法:
public int intValue() {
return value;
}三、自動裝箱和拆箱中的一些問題
1.相同數(shù)值比較返回值為false
public class Main {
public static void main(String[] args) {
Integer num1 = 100;
Integer num2 = 100;
Integer num3 = 200;
Integer num4 = 200;
System.out.println(num1 == num2);
System.out.println(num3 == num4);
}
}
/*
* 運行結(jié)果
* true
* false
*/源碼:
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}從源碼中可以看出,在IntegerCache類中初始化了一個Integer數(shù)組,它的范圍為-128到127。num1==num2在-128到127之間,因此給num1和num2賦值時,直接返回cache[ ]數(shù)組中的對象,屬于同一個對象,返回值為true;而200超過了這個范圍,給num3和num4賦值時,直接返回new Integer(),因此屬于兩個不同的對象,返回值false。
2.浮點型數(shù)值比較返回值為false7
public class Main {
public static void main(String[] args) {
Double num1 = 100.0;
Double num2 = 100.0;
Double num3 = 200.0;
Double num4 = 200.0;
System.out.println(num1 == num2);
System.out.println(num3 == num4);
}
}
/*
* 運行結(jié)果
* false
* false
*/源碼:
public static Double valueOf(double d) {
return new Double(d);
}從源碼中可以看出,Double中的valueOf()返回了一個新的封裝類對象,因此都返回false。
以上就是Java的封裝類和裝箱拆箱詳解的詳細內(nèi)容,更多關(guān)于Java封裝類和裝箱拆箱的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java打印表格 將ResultSet中的數(shù)據(jù)打印成表格問題
這篇文章主要介紹了java打印表格 將ResultSet中的數(shù)據(jù)打印成表格問題。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
SpringCloud微服務(wù)網(wǎng)關(guān)限流方式
這篇文章主要介紹了SpringCloud微服務(wù)網(wǎng)關(guān)限流方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
springcloud下hibernate本地化方言配置方式
這篇文章主要介紹了springcloud下hibernate本地化方言配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

