当前位置:网站首页>Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
2022-07-31 00:15:00 【come on】
目录
What is autoboxing and unboxing?
When autoboxing and unboxing occur?
Check the packaging type11An example of a common test for equality
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() |
自动装箱与拆箱了解吗?原理是什么?
What is autoboxing and unboxing?
- 装箱:将基本类型用它们对应的引用类型包装起来;
- 拆箱:将包装类型转换为基本数据类型;
- 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;
}When autoboxing and unboxing occur?
- 当出现赋值运算、算数表达式、方法调用等情况时,会触发自动装箱/拆箱操作.
- 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;
- 【注意】Objects in the constant pool are only used when autoboxing occurs,So pay attention to the timing of autoboxing and unboxing;
- 两个newThe objects that come out must not be equal;
- 两种浮点数类型的包装类 Float,Double 并没有实现常量池技术.
- 对于对象引用类型:==比较的是对象的内存地址.
- 对于基本数据类型:==比较的是值.
Check the packaging type11An example of a common test for equality
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,发生自动装箱,So it's all in the constant pool
System.out.println("i1=i2+i3 " + (i1 == i2 + i3));// 输出 true, +operator not applicableInteger类型, 所以i2和i3It will be automatically unboxed for numerical addition, 即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, It will be automatically unboxed and converted to int进行比较;
System.out.println("i4=i5 " + (i4 == i5));// 输出 false, new会开辟新的内存空间,故两个newIt will not wait to come out;
System.out.println("i4=i7 " + (i4 == i7));// 输出 true, i7是数值类型, 所以i4会自动拆箱, 然后进行比较;
System.out.println("i4=i5+i6 " + (i4 == i5 + i6));// 输出 true, +operator not applicableInteger类型, 所以i5和i6It will be automatically unboxed for numerical addition, 即i4==40, 作比较的时候i4会自动拆箱,即40==40,true;
System.out.println("40=i5+i6 " + (40 == i5 + i6));// 输出 true, +operator not applicableInteger类型, 所以i5和i6It will be automatically unboxed for numerical addition, 即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 方法比较.

边栏推荐
猜你喜欢
随机推荐
What are the efficient open source artifacts of VSCode
ctfshow 文件包含
软考总结
leetcode:127. Word Solitaire
写了多年业务代码,我发现了这11个门道,只有内行才知道
会员生日提前了一天
.NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform
47. 【Pointers and Arrays】
Axure轮播图
46.<list链表的举列>
如何在WordPress网站上添加导航菜单
joiplay模拟器如何导入游戏存档
Encapsulate and obtain system user information, roles and permission control
Chevrolet Trailblazer, the first choice for safety and warmth for your family travel
flutter 做底部的三个按键,有叠加,有填充
Game mall table establishment
Soft Exam Study Plan
leetcode 406. Queue Reconstruction by Height
2D Transform Module && Media Queries
天空云变化案例









