自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深入理解Java之裝箱與拆箱

開發(fā) 后端
在說裝箱與拆箱之前,先說一下Java的基本數(shù)據(jù)類型,Java從數(shù)據(jù)類型上可以劃分為值類型與引用類型。

一、Java數(shù)據(jù)類型

1、在說裝箱與拆箱之前,先說一下Java的基本數(shù)據(jù)類型,Java從數(shù)據(jù)類型上可以劃分為值類型與引用類型,值類型是四類八種,分別是:

  •  整數(shù)型:byte̵,short̵,int̵,long
  •  浮點(diǎn)型:float,double
  •  字符型:char
  •  布爾型:boolean
數(shù)據(jù)類型 內(nèi)存 默認(rèn)值 包裝類
byte 8位 0 Byte
short 16位 0 short
int 32位 0 Integer
long 64位 0L或0l Long
float 32位 0.0F或0.0f Float
double 64位 0.0D或0.0d Double
char 16位 \u0000 Character
boolean 8位 flase Boolean

2、引用類型:

  •  數(shù)組
  •  類(class)
  •  接口(Interface)
  •  枚舉(enum)

3、值類型與引用類型的區(qū)別

   1.  從概念方面上來說:

  •  值類型:變量名指向具體的值
  •  引用類型:變量名指向數(shù)據(jù)對(duì)象的內(nèi)存地址

   2.  從內(nèi)存構(gòu)建方面上來說:

  •  值類型:變量在聲明之后,Java就會(huì)立刻分配給它內(nèi)存空間
  •  引用類型:它以特殊的方式(類似C指針)指向?qū)ο髮?shí)體,這類變量聲明時(shí)不會(huì)分配內(nèi)存,只是存儲(chǔ)

   3.  從使用方面上來說:

  •  值類型:使用時(shí)需要賦具體值,判斷時(shí)用 ” == “號(hào)
  •  引用類型:使用時(shí)可以賦null,判斷時(shí)使用 equals 方法

二、Java數(shù)據(jù)類型轉(zhuǎn)換

1、自動(dòng)轉(zhuǎn)換

  •     定義:程序在執(zhí)行過程中“悄然”進(jìn)行的轉(zhuǎn)換,不需要用戶提前聲明,一般是從位數(shù)低的類型向位數(shù)高的類型轉(zhuǎn)換
  •     優(yōu)先關(guān)系:按從低到高的順序轉(zhuǎn)換。不同類型數(shù)據(jù)間的優(yōu)先   關(guān)系如下:
    •  低--------------------------------------------->高
    •  byte,short,char-> int -> long -> float -> double
  •  轉(zhuǎn)換規(guī)則:

    運(yùn)算中,不同類型的數(shù)據(jù)先轉(zhuǎn)化為同一類型,然后進(jìn)行運(yùn)算   

操作數(shù)1類型 操作數(shù)2類型 轉(zhuǎn)換后的類型
byte、short、char int int
byte、short、char、int long long
byte、short、char、int、long float float
byte、short、char、int、long、float double double

2、強(qiáng)制轉(zhuǎn)換

  •  定義:強(qiáng)制類型轉(zhuǎn)換則必須在代碼中聲明,轉(zhuǎn)換順序不受限制
  •  格式:在需要轉(zhuǎn)型的數(shù)據(jù)前加上“( )”,然后在括號(hào)內(nèi)加入需要轉(zhuǎn)化的數(shù)據(jù)類型
  •  結(jié)果:精度可能會(huì)丟失,也可能更加精確 
  1. int x;  
  2.  double y;  
  3.  x = (int)3.14 + (int)5.20  //精度丟失  
  4.  y = (double)x + (double)8  //精度提升  
  5.  輸出:x = 8;y = 16.0 

三、Java之裝箱與拆箱

1、包裝類

  •  Java是面向?qū)ο笳Z言,號(hào)稱萬事萬物皆對(duì)象,因此,8種基本數(shù)據(jù)類型有了對(duì)應(yīng)的類,這就是包裝類

2、什么是裝箱與拆箱

  •  裝箱:將值類型裝換成引用類型的過程
  •  拆箱:將引用類型轉(zhuǎn)換成值類型的過程
  •  自動(dòng)裝箱:   
  1. int x = 3 
  2.    Integer y = x;  //int --> Integer,Integer y = x <==> Integer y = Integer.valueOf(x) 
  •  自動(dòng)拆箱:   
  1. Integer x = new Integer(5);  
  2.    int y = x;  //Integer --> int,int y = x <==> int y = x.intValue() 

3、裝箱和拆箱是如何實(shí)現(xiàn)的

  •  裝箱過程是通過調(diào)用包裝器的valueOf方法實(shí)現(xiàn)的
  •  拆箱過程是通過調(diào)用包裝器的 xxxValue方法實(shí)現(xiàn)的。(xxx代表對(duì)應(yīng)的基本數(shù)據(jù)類型)

4、注意點(diǎn):

  1.  大量使用自動(dòng)拆裝箱會(huì)使性能降低,還會(huì)造成大量的內(nèi)存消耗
  2.  在重載方法中,可能出現(xiàn)問題 
  1. List<Integer> list = new ArrayList<>();  
  2.   Integer x,y,z;  
  3.   x = 1;y = 2;z = 4 
  4.   list.add(x);list.add(y);list.add(z);  
  5.   list.remove(2); 

在上面這段代碼中ArrayList.remove方法有兩個(gè)重載方法,那么list.remove(2)是調(diào)用了哪個(gè)方法,remove掉的是值為2的對(duì)象,還是remove了index為2,值為4的那個(gè)對(duì)象呢?

在這種情況下,編譯器不會(huì)進(jìn)行自動(dòng)拆裝箱,所以調(diào)用的是remove(int index),index為2值為4的這個(gè)Integer對(duì)象會(huì)被remove.

如果要調(diào)用 remove(Object o)的方法,應(yīng)該這么寫 list.remove(y)

    3.  緩存值問題

  •  案例解析:   
  1. Integer i1 = 100 
  2.    Integer i2 = 100 
  3.    Integer i3 = 200 
  4.    Integer i4 = 200 
  5.    System.out.println(i1==i2);  
  6.    System.out.println(i3==i4);  
  7.    Output: true false 
  •  觀察源碼:

    Intteger.valueOf方法   

  1. public static Integer valueOf(int i) {  
  2.         if (i >= IntegerCache.low && i <= IntegerCache.high)  
  3.             return IntegerCache.cache[i + (-IntegerCache.low)];  
  4.         return new Integer(i);  
  5.     } 

IntegerCache類   

  1. private static class IntegerCache { 
  2.        static final int low = -128;  
  3.        static final int high;  
  4.        static final Integer cache[];  
  5.        static {  
  6.            // high value may be configured by property  
  7.            int h = 127 
  8.            String integerCacheHighPropValue =  
  9.                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");  
  10.            if (integerCacheHighPropValue != null) {  
  11.                try {  
  12.                    int i = parseInt(integerCacheHighPropValue);  
  13.                    i = Math.max(i, 127);  
  14.                    // Maximum array size is Integer.MAX_VALUE  
  15.                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);  
  16.                } catch( NumberFormatException nfe) {  
  17.                    // If the property cannot be parsed into an int, ignore it.  
  18.                }  
  19.            }  
  20.            hhigh = h;  
  21.            cache = new Integer[(high - low) + 1];  
  22.            int j = low 
  23.            for(int k = 0; k < cache.length; k++)  
  24.                cache[k] = new Integer(j++);  
  25.            // range [-128, 127] must be interned (JLS7 5.1.7)  
  26.            assert IntegerCache.high >= 127;  
  27.        }  
  28.        private IntegerCache() {}  
  29.    } 

從源碼可以看出,在通過valueOf方法創(chuàng)建Integer對(duì)象的時(shí)候,如果數(shù)值在[-128,127]之間,便返回指向IntegerCache.cache中已經(jīng)存在的對(duì)象的引用;否則創(chuàng)建一個(gè)新的Integer對(duì)象

  •  Byte、Short、Integer、Long四種包裝類默認(rèn)創(chuàng)建了數(shù)值為[-128,127]的相應(yīng)類型的緩存數(shù)據(jù),但是超出此范圍仍會(huì)創(chuàng)建新的對(duì)象。
  •  Character默認(rèn)會(huì)創(chuàng)建[0,127]的響應(yīng)類型的緩存數(shù)據(jù)
  •  兩種浮點(diǎn)型沒有實(shí)現(xiàn)常量池技術(shù),在某個(gè)范圍內(nèi)的整型數(shù)值的個(gè)數(shù)是有限的,而浮點(diǎn)數(shù)卻不是   
包裝類 常量池 常量池范圍
Byte 存在 [-128,127]
Short 存在 [-128,127]
Integer 存在 [-128,127]
Long 存在 [-128,127]
Character 存在 [0,127]
Float 不存在
Double 不存在
  •  注意點(diǎn):
    •   當(dāng) "=="運(yùn)算符的兩個(gè)操作數(shù)都是 包裝器類型的引用,則是比較指向的是否是同一個(gè)對(duì)象,而如果其中有一個(gè)操作數(shù)是表達(dá)式(即包含算術(shù)運(yùn)算)則比較的是數(shù)值(即會(huì)觸發(fā)自動(dòng)拆箱的過程)
    •  對(duì)于包裝器類型,equals方法并不會(huì)進(jìn)行類型轉(zhuǎn)換
    •  算術(shù)運(yùn)算會(huì)觸發(fā)裝箱與拆箱過程 

 

責(zé)任編輯:龐桂玉 來源: Linux公社
相關(guān)推薦

2015-09-02 10:12:54

Java自動(dòng)裝箱拆箱

2021-09-06 14:30:34

C#裝箱拆箱

2009-08-26 03:39:00

C#裝箱和拆箱

2012-03-26 11:32:45

Java

2021-02-17 11:25:33

前端JavaScriptthis

2009-08-28 11:22:11

C#裝箱和拆箱

2009-08-06 15:40:11

C#裝箱和拆箱

2010-03-12 08:55:06

Java內(nèi)省反射

2014-12-04 14:01:54

openstacknetworkneutron

2018-12-27 12:34:42

HadoopHDFS分布式系統(tǒng)

2019-03-18 09:50:44

Nginx架構(gòu)服務(wù)器

2022-09-05 22:22:00

Stream操作對(duì)象

2017-11-14 14:41:11

Java泛型IO

2009-09-01 17:51:47

C#拆箱C#裝箱

2024-03-15 15:03:23

2024-06-28 10:25:18

2010-06-01 15:25:27

JavaCLASSPATH

2016-12-08 15:36:59

HashMap數(shù)據(jù)結(jié)構(gòu)hash函數(shù)

2020-07-21 08:26:08

SpringSecurity過濾器

2014-12-01 15:38:33

openstacknetworkneutron
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)