当前位置:网站首页>解释一下自动装箱和自动拆箱?
解释一下自动装箱和自动拆箱?
2022-07-26 09:40:00 【华为云】
解释一下自动装箱和自动拆箱?
自动装箱:将基本数据类型重新转化为对象
public class Test { public static void main(String[] args) { // 声明一个Integer对象,用到了自动的装箱:解析为:Integer num = Integer.valueOf(9); Integer num = 9; } } 9是属于基本数据类型的,原则上它是不能直接赋值给一个对象Integer的。但jdk1.5 开始引入了自动装箱/拆箱机制,就可以进行这样的声明,自动将基本数据类型转化为对应的封装类型,成为一个对象以后就可以调用对象所声明的所有的方法。
自动拆箱:将对象重新转化为基本数据类型
public class Test {
public static void main(String[] args) {
/ /声明一个Integer对象
Integer num = 9;
// 进行计算时隐含的有自动拆箱 System.out.print(num--); } } 因为对象时不能直接进行运算的,而是要转化为基本数据类型后才能进行加减乘除。
int 和 Integer 有什么区别?
Integer是int的包装类;int是基本数据类型;
Integer变量必须实例化后才能使用;int变量不需要;
Integer实际是对象的引用,指向此new的Integer对象;int是直接存储数据值 ;
Integer的默认值是null;int的默认值是0。
两个new生成的Integer变量的对比
由于Integer变量实际上是对一个Integer对象的引用,所以两个通过new生成的Integer变量永远是不相等的(因为new生成的是两个对象,其内存地址不同)。
Integer i = new Integer(10000);
Integer j = new Integer(10000);
System.out.print(i == j); //false
Integer变量和int变量的对比
Integer变量和int变量比较时,只要两个变量的值是向等的,则结果为true(因为包装类Integer和基本数据类型int比较时,java会自动拆包装为int,然后进行比较,实际上就变为两个int变量的比较)
int a = 10000;Integer b = new Integer(10000);Integer c=10000;System.out.println(a == b); // trueSystem.out.println(a == c); // true非new生成的Integer变量和new Integer()生成变量的对比
非new生成的Integer变量和new Integer()生成的变量比较时,结果为false。(因为非new生成的Integer变量指向的是java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同)
Integer b = new Integer(10000);Integer c=10000;System.out.println(b == c); // false
两个非new生成的Integer对象的对比
对于两个非new生成的Integer对象,进行比较时,如果两个变量的值在区间-128到127之间,则比较结果为true,如果两个变量的值不在此区间,则比较结果为false
Integer i = 100;
Integer j = 100;
System.out.print(i == j); //true
Integer i = 128;
Integer j = 128;
System.out.print(i == j); //false
当值在 -128 ~ 127之间时,java会进行自动装箱,然后会对值进行缓存,如果下次再有相同的值,会直接在缓存中取出使用。缓存是通过Integer的内部类IntegerCache来完成的。当值超出此范围,会在堆中new出一个对象来存储。
给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf,源码如下:
public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s,radix));
}
1
2
/**
(1)在-128~127之内:静态常量池中cache数组是static final类型,cache数组对象会被存储于静态常量池中。
cache数组里面的元素却不是static final类型,而是cache[k] = new Integer(j++),
那么这些元素是存储于堆中,只是cache数组对象存储的是指向了堆中的Integer对象(引用地址)
(2)在-128~127 之外:新建一个 Integer对象,并返回。
*/
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high) {
return IntegerCache.cache[i + (-IntegerCache.low)];
}
return new Integer(i);
}
IntegerCache是Integer的内部类,源码如下:/**
缓存支持自动装箱的对象标识语义 -128和127(含)。
缓存在第一次使用时初始化。 缓存的大小可以由-XX:AutoBoxCacheMax = <size>选项控制。
在VM初始化期间,java.lang.Integer.IntegerCache.high属性可以设置并保存在私有系统属性中
*/
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) {
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);
}
high = h;cache = new Integer[(high - low) + 1];int j = low;for(int k = 0; k < cache.length; k++) { cache[k] = new Integer(j++); // 创建一个对象}}
private IntegerCache() {}
}
什么是反射?
反射是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为 Java 语言的反射机制。
反射机制的优缺点有哪些?
优点:能够运行时动态获取类的实例,提高灵活性;可与动态编译结合Class.forName(‘com.mysql.jdbc.Driver.class’);,加载MySQL的驱动类。
缺点:使用反射性能较低,需要解析字节码,将内存中的对象进行解析。其解决方案是:通过setAccessible(true)关闭JDK的安全检查来提升反射速度;多次创建一个类的实例时,有缓存会快很多;ReflflectASM工具类,通过字节码生成的方式加快反射速度。
边栏推荐
- The problem of accessing certsrv after configuring ADCs
- Gauss elimination solves the inverse of matrix (Gauss)
- Solve NPM -v sudden failure and no response
- 解决npm -v突然失效 无反应
- (一)面扫描仪与机械臂的手眼标定(眼在手上)
- Interview shock 68: why does TCP need three handshakes?
- [MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
- Fiddler下载安装
- 服务器、客户端双认证(2)
- Search module use case writing
猜你喜欢

uni-app学习总结

Node memory overflow and V8 garbage collection mechanism

Solve NPM -v sudden failure and no response

Redis sentinel mode setup under Windows

高斯消元

Customize permission validation in blazor

Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning

小白搞一波深拷贝 浅拷贝

Logical architecture of MySQL

Qt随手笔记(三)在vs中使用QtCharts画折线图
随机推荐
高斯消元求解矩阵的逆(gauss)
asp.net 使用redis缓存(二)
搜索模块用例编写
JS 一行代码 获取数组最大值与最小值
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
OpenCV 表格识别之表格提取(二)
IIS网站配置
网络流学习笔记
EOJ 2020 1月月赛 E数的变换
After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
Gauss elimination for solving XOR linear equations
Basic knowledge of website design
A new paradigm of distributed deep learning programming: Global tensor
MySQL的逻辑架构
Does volatile rely on the MESI protocol to solve the visibility problem? (top)
Customize permission validation in blazor
2020-12-29
RMQ学习笔记
音视频知识