Java基本类型数据类型、包装类及自动拆装箱举例分析
创始人
2024-12-21 21:02:29
0次

前言

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

那么为什么需要包装类?

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

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

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

拆箱与装箱

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

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

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

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

自动拆箱与自动装箱

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

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

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}. */public int intValue() { return value;}

/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, 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. * * @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”,进行自动拆箱

注意的点

在上述的代码中,关于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源码中可以得到答案

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. }}

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

与之类似还有:

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

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

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

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

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...