包装类

在Java5 中添加了两个新特性,那就是自动装箱和拆箱,因为基本类型的广泛使用,但是Java 又是面向对象的语言,所以提供了包装类型的支持

我们知道基本数据类型包括byte, short, int, long, float, double, char, boolean,对应的包装类分别是Byte, Short, Integer, Long, Float, Double, Character, Boolean。关于基本数据类型的介绍可参考八大基本数据类型

在这一节里我们主要以Integer和int 进行讲解,其他的可以类比

那么为什么需要包装类

JAVA是面向对象的语言,很多类和方法中的参数都需使用对象(例如集合),但基本数据类型却不是面向对象的,这就造成了很多不便

如:List<int> = new ArrayList<>();,就无法编译通过

为了解决该问题,我们引入了包装类,顾名思义,就是将基本类型“包装起来“,使其具备对象的性质,包括可以添加属性和方法,位于java.lang包下。

拆箱与装箱

既然有了基本数据类型和包装类,就必然存在它们之间的转换,如:

public static void main(String[] args) {Integer a = 0;for(int i = 0; i < 100; i++){a += i;}
}

将基本数据类型转化为对应类型的包装类的过程叫“装箱”;将包装类转为对应类型的基本数据类型的过程叫“拆箱

在上面中给变量a赋值的时候就发生了装箱,而在参与计算的过程中就发生了拆箱,但是我们好像什么都没做,这句是自动拆装箱

下面我们再给一个例子

装箱

int num = 25;
Integer i = Integer.valueOf(num);
Integer i = 25;

拆箱

Integer i = new Integer(10);
//unboxing
int num = i;
Float f = new Float(56.78);
float fNum = f;

自动拆箱与自动装箱

Java为了简便拆箱与装箱的操作,提供了自动拆装箱的功能,这极大地方便了程序员们。那么到底是如何实现的呢?。这个特性是在java5中引入的

上面的例子就是一个自动拆箱与装箱的过程,通过反编译工具我们得到,

public static void main(String[] args) {Integer a = Integer.valueOf(0);for (int i = 0; i < 100; i++) {a = Integer.valueOf(a.intValue() + i);}
}

我们不难发现,主要调用了两个方法,Integer.intValue() 和 Integer.valueOf( int i) 方法

查看Integer源码,我们找到了对应代码:

/*** Returns the value of this {@code Integer} as an {@code int}.* 返回一个 Integer 的int 值*/public int intValue() {return value;}
/*** Returns an {@code Integer} instance representing the specified {@code int} value.  * 返回一个代表特定int 值的 Integer 对象* If a new {@code Integer} instance is not required, this method should generally be used in preference to the constructor {@link #Integer(int)}, * 如果不需要一个新的Integer 实例,则通常应优先使用此方法而不是构造函数* as this method is likely to yield significantly better space and time performance by caching frequently requested values.* 因为这种方法可以通过缓存频繁请求的值来获得更好的空间和时间性能。* This method will always cache values in the range -128 to 127,inclusive, and may cache other values outside of this range.* 此方法将始终缓存-128到127(含)范围内的值,并可能缓存此范围之外的其他值。* @param  i an {@code int} value.* @return an {@code Integer} instance representing {@code i}.* @since  1.5*/
public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);
}

很明显,我们我们得出,Java帮你隐藏了内部细节。拆箱的过程就是通过Integer 实体调用intValue()方法;装箱的过程就是调用了 Integer.valueOf(int i) 方法,帮你直接new了一个Integer对象

什么时候进行自动拆装箱?

其实很简单(就是在需要对象的时候就装箱需要基本类型的时候就拆箱)

1.添加到集合中时,进行自动装箱

2.涉及到运算的时候,“加,减,乘, 除” 以及 “比较 equals,compareTo”,进行自动拆箱

例如下面这一段代码,则发生了自动装箱

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 10; i++){li.add(i);
}

注意的点

在上述的代码中,关于Integer valueOf(int i)方法中有IntegerCache类,在自动装箱的过程中有个条件判断

if (i >= IntegerCache.low && i <= IntegerCache.high)

结合注释

This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range

大意是:该方法总是缓存-128 到 127之间的值,同时针对超出这个范围的值也是可能缓存的。

那么为什么可能缓存?其实在IntegerCache源码中可以得到答案

/*** Cache to support the object identity semantics of autoboxing for values between* -128 and 127 (inclusive) as required by JLS.** The cache is initialized on first usage.  The size of the cache* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.* During VM initialization, java.lang.Integer.IntegerCache.high property* may be set and saved in the private system properties in the sun.misc.VM class.*/private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint 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_VALUEh = 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() {}
}

因为缓存最大值是可以配置的。这样设计有一定好处,我们可以根据应用程序的实际情况灵活地调整来提高性能。

可以在vm中修改即可调整缓存大小 java
-Djava.lang.Integer.IntegerCache.high = xxxx Aclass.class 或者 -XX:AutoBoxCacheMax=xxxx

但是这里有问题需要注意,就是从上面代码的逻辑我们可看出,我们只能设置high而不能设置low,还有就是如果high 小于127 将不能生效,其中的low的下限是最开始的时候就固定死了的 final int low = -128;而其中的high虽然是设置了final int的,但是最初的时候,是还没有赋值的;所以我们可以通过设置 java
-Djava.lang.Integer.IntegerCache.high = xxxx Aclass.class 或者 -XX:AutoBoxCacheMax=xxxx

接下来我们通过一段代码进行验证一下,首先我们看一下代码,当运行之后我们可以设置参数-XX:AutoBoxCacheMax=1000 再运行一次

public class TestAutoBoxCache {@Testpublic void test() {Integer a = 127;Integer b = 127;System.out.println(a == b);Integer c = 128;Integer d = 128;System.out.println(c == d);Integer e = 1000;Integer f = 1000;System.out.println(e == f);Integer g = 1001;Integer h = 1001;System.out.println(g == h);Integer i = 20000;Integer j = 20000;System.out.println(i == j);}
}
// 没有设置参数之前的输出
true
false
false
false
false
// 设置参数之后的输出 -XX:AutoBoxCacheMax=1000
true
true
true
false
false  

因为缓存设置的实1000,也就是在[-128,1000] 之间的就缓存了,也验证了我们上面的说法是正确的

与之类似的缓存还有:

Byte与ByteCache,缓存值范围-128到127,固定不可配置(其实就是全部缓存了)

Short与ShortCache,缓存值范围-128到127,固定不可配置

Long与LongCache,缓存值范围-128到127,固定不可配置

Character与CharacterCache,缓存值范围0到127,固定不可配置

包装类型与switch

public static void main(String[] args) {int iVal = 7;switch(iVal) {case 1: System.out.println("First step");break;case 2: System.out.println("Second step");break;default: System.out.println("There is some error");}
}

包装类型和基本类型一样,或者说是和String 一样都是支持switch 的

总结

  1. 为什么需要包装类:JAVA是面向对象的语言,很多类和方法中的参数都需使用对象(例如集合),但基本数据类型却不是面向对象的,这就造成了很多不便
  2. 拆装箱的概念:将基本数据类型转为包装类的过程叫“装箱”,将包装类转为基本数据类型的过程叫“拆箱
  3. 自动拆装箱:Java为了简便拆箱与装箱的操作,提供了自动拆装箱的功能,对于Integer, 拆箱的过程就是通过Integer 实体调用intValue()方法;装箱的过程就是调用了 Integer.valueOf(int i) 方法,帮你直接new了一个Integer对象
  4. 建议使用valueOf() 方法创建一个包装类实例而不是直接使用构造方法,因为该方法可以走缓存提高性能

Java数据类型系列之包装类相关推荐

  1. Java数据类型系列之BigDecimal

    BigDecimal 这篇文章我们会介绍一下Java 中的BigDecimal,并且会通过一些例子演示它的用法,例如精度的操作 Java在java.math包中提供的API类BigDecimal,用来 ...

  2. push方法java_万字长文深入浅出谈Java数据类型系列之Stack

    一. Stack 初识 Java 集合框架提供了一个集合Stack,它提供了stack 数据结构的功能,Java 中也提供了其他很多这样的集合,这种集合完成了某种数据结构的功能 1. stack 数据 ...

  3. java泛型一定用包装类_你不知道的基本数据类型和包装类

    你不知道的基本数据类型和包装类 基本数据类型 Java 基本数据按类型可以分为四大类:布尔型.整数型.浮点型.字符型,这四大类包含 8 种基本数据类型.布尔型:boolean 整数型:byte.sho ...

  4. 【Java基础系列教程】第十四章 Java 包装类、日期与日期格式化类、数值格式化等常用类详解

    一.包装类 1.1 什么是包装类 Java中的数据类型分为基本数据类型和引用数据类型,其中基本数据类型是不具有对象特征的,也就是说它们不能像对象一样拥有属性和方法,以及对象化交互.           ...

  5. Java八种基本数据类型对应的包装类

    目录 前言: 八种基本数据类型对应的包装类: 包装类的常用方法: 有关包装类的注意事项: 参考文献: 前言: 思考:有没有这种需求:调用doSome()方法的时候需要传一个数字进去.但是数字属于基本数 ...

  6. 为什么需要java包装类,装箱拆箱,基本类型与字符串之间的转换,java数据类型

    为什么需要包装类? 首先,Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,将每个基本数据类型设计一个对应的类进行代表,这种方式增强了Java面向对象的性质. 其次,如果 ...

  7. java基本数据类型自动转包装类,Java String和基本数据类型之间的转换(包装类)

    一.String 转化成 基本数据类型 利用基本数据类型对应的包装类的parseXxx() 或 valueOf() 方法 注意 : String 对象的字面量 是 数字类型.否则会报异常(Number ...

  8. java学习笔记(十一)基本数据类型的对象包装类

    基本数据类型的对象包装类 --------------------------- 基本数据类型      包装类   boolean         Boolean   byte            ...

  9. Java入门系列——Java语言基础(小康小白)

    我是小康小白,一个平平无奇的Java,Python小白.热爱有趣的文字,生活和远方. 个人博客:https://blog.csdn.net/weixin_45791445 有问题欢迎QQ联系:1059 ...

最新文章

  1. 今日头条反爬措施形同虚设,论多平台协同在安全方面的重要性
  2. 自定义Android菜单背景
  3. JavaScript对象理解
  4. VC++/MFC中调用CHM帮助文档的方法--ShellExecute
  5. 安川g7接线端子图_ABB、KUKA、FANUC、安川四大家族机器人安全回路小结
  6. The number of requested virtual cores per node 3 exceeds the maximum number of virtual cores 2
  7. mlp神经网络_白天鹅黑天鹅灰天鹅?卷积神经网络帮你搞定识别
  8. linux int64_t 头文件,这对int64_t的处理是GCC和Clang的错误吗?
  9. python是开源语言吗c,属于新十年的开发语言:Go语言可能很快会取代Python
  10. 万圣节海报设计没有思路?看看这些有趣的万圣节狂欢是如何完成的!
  11. 【转】BW的星型数据模型
  12. 拓端tecdat|Python实现谱聚类Spectral Clustering算法和改变簇数结果可视化比较
  13. E-prime debug
  14. c语言中m的ascii值,M的ASCII码值为多少
  15. bilibil网站采集 返回视频下载地址【代码】
  16. k8s重启节点状态NotReady
  17. 网易历届笔试题(1)
  18. SqlException:ConnectionTimeout Expired. The timeout period elapsed during the post-login phase
  19. pip install psycopg2报错
  20. PTX/CR偶联物胶束/TAT-KLA/阿司匹林偶联/双氢青蒿偶联紫杉醇的制备

热门文章

  1. 期刊投稿状态_期刊投稿后的7种状态,如何应对
  2. arduino 328P的BootLoader
  3. 合并多个文件的内容到一个文件
  4. 图表说话之解析excel商务饼图做法
  5. Bootstrap相关整理、免费主题整理
  6. python在大数据分析中的应用
  7. 网页整体布局基本模板
  8. Solana之旅1:Solana是什么
  9. CAD 查找指定部件数量
  10. mac上使用dbeaver设置字体大小