当前位置:网站首页>从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术
从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术
2022-07-31 00:01:00 【加油当当】
目录
Java中的包装类都是那些?
byte | short | int | long | float | double | char | boolean | |
位数 | 8 | 16 | 32 | 64 | 32 | 64 | 16 | 1 |
字节数 | 1 | 2 | 4 | 8 | 4 | 8 | 2 | 1(1/8) |
默认值 | 0 | 0 | 0 | 0L | 0.0f | 0.0d | false | |
包装类型 | Byte | Short | Integer | Long | Float | Double | Character | Boolean |
包装类 | 包装类转基本类型 | 基本类型转包装类 |
Byte | Byte.valueOf(byte) | byteInstance.byteValue() |
Short | Short.valueOf(short) | shortInstance.shortValue() |
Integer | Integer.valueOf(int) | integerInstance.intValue() |
Long | Long.valueOf(long) | longInstance.longValue() |
Float | Float.valueOf(float) | floatInstance.floatValue() |
Double | Double.valueOf(double) | doubleInstance.doubleValue() |
Character | Character.valueOf(char) | charInstance.charValue() |
boolean | Boolean.valueOf(booleann) | booleanInstance.booleanValue() |
自动装箱与拆箱了解吗?原理是什么?
自动装箱与拆箱是什么?
- 装箱:将基本类型用它们对应的引用类型包装起来;
- 拆箱:将包装类型转换为基本数据类型;
- int类型与Integer类型比较时, 先将Integer拆箱, 再比较值;
举例:
Integer i = 10; //装箱
int n = i; //拆箱
原理
装箱其实就是调用了 包装类的valueOf()方法,拆箱其实就是调用了 xxxValue()方法。
因此,
Integer i = 10 等价于 Integer i = Integer.valueOf(10)
int n = i 等价于 int n = i.intValue();
注意:如果频繁拆装箱的话,也会严重影响系统的性能。我们应该尽量避免不必要的拆装箱操作。
private static long sum() {
// 应该使用 long 而不是 Long
Long sum = 0L;
for (long i = 0; i <= Integer.MAX_VALUE; i++)
sum += i;
return sum;
}自动装箱与拆箱的发生时机?
- 当出现赋值运算、算数表达式、方法调用等情况时,会触发自动装箱/拆箱操作。
- Byte b,
- b=b++;
包装类型的常量池技术了解么?【必会】【笔试会考】
- Java 基本类型的包装类的大部分都实现了常量池技术。
- Byte,Short,Integer,Long 这 4 种包装类默认创建了数值 [-128,127] 的相应类型的缓存数据,Character 创建了数值在 [0,127] 范围的缓存数据,Boolean 直接返回 True or False。
- 如果超出对应范围仍然会去创建新的对象,缓存的范围区间的大小只是在性能和资源之间的权衡。
- 如果整型字面量的值在-128到127之间,那么自动装箱时不会new新的Integer 对象,而是直接引用常量池中的Integer对象,超过范围 a1==b1的结果是false;
- 【注意】发生自动装箱时才会用常量池中的对象,所以要注意自动装箱和拆箱的时机;
- 两个new出来的对象一定不相等;
- 两种浮点数类型的包装类 Float,Double 并没有实现常量池技术。
- 对于对象引用类型:==比较的是对象的内存地址。
- 对于基本数据类型:==比较的是值。
考察包装类型的11个常见判断是否相等的例子
Integer i1 = 40;
Integer i2 = 40;
Integer i3 = 0;
Integer i4 = new Integer(40);
Integer i5 = new Integer(40);
Integer i6 = new Integer(0);
int i7 = 40;
System.out.println("i1=i2 " + (i1 == i2));// 输出 true,发生自动装箱,所以都是在常量池中
System.out.println("i1=i2+i3 " + (i1 == i2 + i3));// 输出 true, +操作符不适用Integer类型, 所以i2和i3会自动拆箱进行数值相加, 即i1==40, 作比较的时候i1会自动拆箱,即40==40,true;
System.out.println("i1=i4 " + (i1 == i4));// 输出 false, i1是自动装箱产生的, 而i4是new出来的, 一个在常量池, 一个在堆, 故不等;
System.out.println("i1=i7 " + (i1 == i7));// 输出 true, 会自动拆箱转换成int进行比较;
System.out.println("i4=i5 " + (i4 == i5));// 输出 false, new会开辟新的内存空间,故两个new出来的必不等;
System.out.println("i4=i7 " + (i4 == i7));// 输出 true, i7是数值类型, 所以i4会自动拆箱, 然后进行比较;
System.out.println("i4=i5+i6 " + (i4 == i5 + i6));// 输出 true, +操作符不适用Integer类型, 所以i5和i6会自动拆箱进行数值相加, 即i4==40, 作比较的时候i4会自动拆箱,即40==40,true;
System.out.println("40=i5+i6 " + (40 == i5 + i6));// 输出 true, +操作符不适用Integer类型, 所以i5和i6会自动拆箱进行数值相加, 即i1==40, 作比较的时候i1会自动拆箱,即40==40,true
Integer a1 = 128;
Integer b1 = 128;
System.out.println(a1 == b1); // false
Integer a2 = 127;
Integer b2 = 127;
System.out.println(a2 == b2); // true
Float i11 = 333f;
Float i22 = 333f;
System.out.println(i11 == i22);// 输出 false
Double i3 = 1.2;
Double i4 = 1.2;
System.out.println(i3 == i4);// 输出 false记住:所有整型包装类对象之间值的比较,全部使用 equals 方法比较。

边栏推荐
猜你喜欢

Mysql体系化之JOIN运算实例分析

JS中? ?和??=和?.和 ||的区别

Machine Learning 1-Regression Model (2)

SWM32系列教程6-Systick和PWM

How to import game archives in joiplay emulator

How to solve the error of joiplay simulator

递增三元组

Chevrolet Trailblazer, the first choice for safety and warmth for your family travel

Flex布局使用

边缘计算与小程序也能结合!智能家居是否能借势上台阶
随机推荐
乌克兰外交部:乌已完成恢复粮食安全出口的必要准备
Encapsulate and obtain system user information, roles and permission control
Chevrolet Trailblazer, the first choice for safety and warmth for your family travel
宽客必备神器-AKShare
Ukraine's foreign ministry: wu was restored to complete the export of food security
xss靶机训练【实现弹窗即成功】
VSCode高效开源神器有哪些
[0x800706D9] solution appears in Microsoft Store
background对float的子元素无效
软件测试三阶段,你在哪一步?
ABC 261 F - Sorting Color Balls(逆序对)
image里的mode属性
ctfshow 文件包含
Calico 网络通信原理揭秘
10 个关于自动化发布管理的好处
asser利用蚁剑登录
"Wei cup" school more than 2022 cattle summer camp 4 L.B lack Hole, computational geometry
Shell programming conditional statement test command Integer value, string comparison Logical test File test
jira是什么
MySQL数据库的truncate与delete区别