最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

java包裝類(lèi)的自動(dòng)封裝、自動(dòng)拆箱和緩存

2022-07-12 17:28 作者:虛云幻仙  | 我要投稿

/**
* 包裝類(lèi)的自動(dòng)封裝、自動(dòng)拆箱和緩存
*/

public class TestWrapper2 {
? ?public static void main(String[] args) {
? ? ? ?Integer i1 = 1;
? ? ? ?//自動(dòng)封裝 通過(guò)編譯器編譯后轉(zhuǎn)變?yōu)?Integer i1 = Integer.valueOf(1); 供虛擬機(jī)執(zhí)行
? ? ? ?//Integer i1 引用類(lèi)型變量

? ? ? ?int i2 = i1;
? ? ? ?//自動(dòng)拆箱 通過(guò)編譯器編譯后轉(zhuǎn)變?yōu)?int i2 = i1.intValue(); 供虛擬機(jī)執(zhí)行
? ? ? ?//int i2 基本數(shù)據(jù)類(lèi)型

? ? ? ?Integer i3 = null;
? ? ? ?i2 = i3;
? ? ? ?//運(yùn)行時(shí)報(bào)錯(cuò)NullPointerException 在虛擬機(jī)運(yùn)行時(shí)代碼已經(jīng)變?yōu)?i2 = i3.intValue() 但i3為null空指針 沒(méi)有指向?qū)ο缶驼也坏?intValue()方法

? ? ? ?Integer i4 = 4000;
? ? ? ?Integer i5 = 4000;
? ? ? ?System.out.println(i4==i5);
? ? ? ?//結(jié)果false i4 = Integer.valueOf(4000) Integer靜態(tài)方法.valueOf()執(zhí)行后會(huì)new一個(gè)新的對(duì)象給i4 value值為4000
? ? ? ?//i5 = Integer.valueOf(4000) 同樣會(huì)new一個(gè)新的對(duì)象給i5 ?i4和i5分別指向兩個(gè)對(duì)象

? ? ? ?System.out.println(i4.equals(i5));
? ? ? ?//結(jié)果true Integer重寫(xiě)的equals()方法 在判斷i5 instanceof Integer 判斷i5是Integer的實(shí)例后比較i4和i5的value 兩個(gè)value都是4000所以true
? ? ? ?Integer i6 = 123;
? ? ? ?Integer i7 = 123;
? ? ? ?System.out.println(i6==i7);
? ? ? ?//結(jié)果true 原因:在Integer初始化時(shí)緩存了整數(shù)范圍在[-128,127]之間的包裝類(lèi)對(duì)象
? ? ? ?//當(dāng)i6 = Integer.valueOf(123)時(shí) 方法沒(méi)有new新的包裝類(lèi)對(duì)象 而是將緩存中提前創(chuàng)建好的value=123的對(duì)象返回給i6
? ? ? ?//i7 = 123同理 ?i6和i7指向的是同一個(gè)對(duì)象

? ? ? ?/*
? ? ? ? ? ?public static Integer valueOf(int i) {
? ? ? ? ? ?//靜態(tài)方法valueOf() 返回Integer類(lèi)對(duì)象
? ? ? ? ? ? ? ?if (i >= IntegerCache.low && i <= IntegerCache.high)
? ? ? ? ? ? ? ?//IntegerCache整數(shù)緩存 .low=-128 .high=127 系統(tǒng)運(yùn)行時(shí)已經(jīng)提前建好了-128到127的整數(shù)的包裝類(lèi)放到了緩存數(shù)組中
? ? ? ? ? ? ? ? ? ?return IntegerCache.cache[i + (-IntegerCache.low)];
? ? ? ? ? ? ? ? ? ?//當(dāng)判斷i在-128到127之間 返回對(duì)應(yīng)的對(duì)象
? ? ? ? ? ? ? ?return new Integer(i);
? ? ? ? ? ? ? ?//當(dāng)i不在緩存數(shù)組的范圍內(nèi)是new對(duì)象返回
? ? ? ? */


? ?}
}

class ImitateInteger {
? ?//imitate模仿Integer緩存數(shù)組
? ?private int value;
? ?//每個(gè)對(duì)象對(duì)應(yīng)一個(gè)value值
? ?private ImitateInteger(int i){
? ? ? ?//將構(gòu)造器私有 外部生成對(duì)象時(shí)需要通過(guò).valueOf()方法
? ? ? ?this.value = i;
? ?}
? ?private static final int LOW = -128;
? ?//模擬[-128,127] 兩個(gè)值設(shè)為final常量 private僅方法內(nèi)判斷時(shí)調(diào)用 static這個(gè)屬性不需要給對(duì)象
? ?private static final int HIGH = 127;
? ?private static final ImitateInteger[] cache = new ImitateInteger[HIGH-LOW+1];
? ?//創(chuàng)建一個(gè)數(shù)組準(zhǔn)備緩存[-128,127]之間的對(duì)象 -128到-1是128個(gè) 1到127是127個(gè) 再加一個(gè)0 [127-(-128)+1]
? ?//換一種想法 從HIGH到LOW總共有HIGH-LOW個(gè)數(shù) 包含HIGH沒(méi)包含LOW 所以再+1

? ?static {
? ? ? ?//靜態(tài)初始化塊 在類(lèi)加載時(shí)執(zhí)行
? ? ? ?for (int i=LOW;i<=HIGH;i++){
? ? ? ? ? ?cache[i+(-LOW)] = new ImitateInteger(i);
? ? ? ? ? ?//數(shù)組的index范圍[0]-[255] i=LOW i-LOW=0 ?i=HIGH i-LOW=255
? ? ? ? ? ?//將value=-128的對(duì)象交給cache[0] ? value=127的對(duì)象交給cache[255]
? ? ? ? ? ?//在類(lèi)加載時(shí)即緩存new了-128到127

? ? ? ?}
? ?}

? ?public static ImitateInteger valueOf(int i){
? ? ? ?//public供外部調(diào)用的代替私有構(gòu)造器的方法 代替構(gòu)造器所以返回ImitateInteger對(duì)象
? ? ? ?if (i>=LOW&&i<=HIGH){
? ? ? ? ? ?//判斷i是否在[-128,127]區(qū)間
? ? ? ? ? ?return cache[i+(-LOW)];
? ? ? ? ? ?//對(duì)應(yīng)靜態(tài)初始化快中的緩存數(shù)組 i對(duì)應(yīng)的對(duì)象的index要加128即(-LOW)
? ? ? ?}
? ? ? ?return new ImitateInteger(i);
? ? ? ?//不在區(qū)間內(nèi)則new新對(duì)象返回
? ?}

? ?public static void main(String[] args) {
? ? ? ?ImitateInteger i1 = ImitateInteger.valueOf(1);
? ? ? ?ImitateInteger i2 = ImitateInteger.valueOf(1);
? ? ? ?System.out.println(i1==i2);
? ? ? ?//結(jié)果true i1和i2都沒(méi)有new對(duì)象 返回了cache[1+(-(-128))]即cache[129] ?cache[129].value對(duì)應(yīng)1

? ? ? ?ImitateInteger i3 = ImitateInteger.valueOf(200);
? ? ? ?ImitateInteger i4 = ImitateInteger.valueOf(200);
? ? ? ?System.out.println(i3==i4);
? ? ? ?//結(jié)果false i3和i4分別指向new新生成的對(duì)象
? ?}

? ?public boolean equals(Object o){
? ? ? ?//重寫(xiě)繼承自O(shè)bject 的equals()
? ? ? ?if (o instanceof ImitateInteger){
? ? ? ? ? ?return this.value==((ImitateInteger)o).value;
? ? ? ? ? ?//比較兩個(gè)對(duì)象的value ?將o強(qiáng)轉(zhuǎn)為ImitateInteger
? ? ? ?}
? ? ? ?return false;
? ? ? ?//不是同類(lèi)返回false
? ? ? ?// this.equals(o); 用來(lái)比較兩個(gè)對(duì)象所以不是static方法

? ?}

? ?public int intValue(){
? ? ? ?return this.value;
? ? ? ?//int a = i4.intValue(); 將包裝類(lèi)轉(zhuǎn)為基本數(shù)據(jù)類(lèi)型 要轉(zhuǎn)換的是對(duì)象 所以不用static
? ?}

? ?@Override
? ?public String toString() {
? ? ? ?//重寫(xiě)toString
? ? ? ?return ""+this.value;
? ? ? ?//返回值只能是字符串
? ?}
}

java包裝類(lèi)的自動(dòng)封裝、自動(dòng)拆箱和緩存的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
织金县| 来凤县| 会宁县| 玉门市| 双流县| 稻城县| 达州市| 武乡县| 西充县| 天津市| 福海县| 镇坪县| 砚山县| 奉节县| 澄迈县| 资溪县| 修水县| 信阳市| 巴彦县| 栖霞市| 府谷县| 仁化县| 望奎县| 溧水县| 六安市| 清丰县| 突泉县| 达孜县| 灵璧县| 广州市| 彩票| 滨州市| 丰宁| 宁都县| 明水县| 肇州县| 西藏| 龙海市| 太仆寺旗| 丁青县| 营口市|