当前位置:网站首页>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 方法比较.
边栏推荐
- After writing business code for many years, I found these 11 doorways, which only experts know
- Word文件损坏如何修复
- Machine Learning 1-Regression Model (2)
- Android安全性优化——APP加固
- @requestmapping注解的作用及用法
- Learn Scope from a Compilation Perspective!
- 【深入浅出玩转FPGA学习15----------时序分析基础】
- 【深入浅出玩转FPGA学习13-----------测试用例设计1】
- Chevrolet Trailblazer, the first choice for safety and warmth for your family travel
- 天空云变化案例
猜你喜欢
mysql 中手动设置事务提交
binglog log tracking: data backup and backup tracking
Shell脚本 if语句
Oracle一个诡异的临时表空间不足的问题
xss绕过:prompt(1)
transition transition && animation animation
Homework: iptables prevent nmap scan and binlog
what is jira
Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.
asser利用蚁剑登录
随机推荐
封装、获取系统用户信息、角色及权限控制
PHP图片添加文字水印
IOT跨平台组件设计方案
Manually set transaction commit in mysql
45. [Application of list linked list]
数据库的严格模式
H5跳转微信公众号解决方案
MySQL的grant语句
Unity 加载读取PPT
网络常用的状态码
After writing business code for many years, I found these 11 doorways, which only experts know
binglog日志追踪:数据备份并备份追踪
常用的正则表达式
uniapp folding box secondary loop
46.<list链表的举列>
Necessary artifacts - AKShare quants
leetcode:127. Word Solitaire
software development design process
Point Cloud Scene Reconstruction with Depth Estimation
MySQL中substring与substr区别