【Java基础】包装类的使用
目录
1.包装类是什么
基本数据类型变为引用数据类型,每一个都是一个类。
Integer age = new Integer(18); int age = 18;
2.为什么要用包装类
(1)使基本数据类型具有类的特点,创建对象,调用类的方法,符合面向对象。
(2)基本数据类型的默认值各不相同int的为0,double为0.0...,而对象的默认值都为null,非常方便用来做判空操作,数据库中数据为空的话,使用int类型接收会报错。
(3)阿里巴巴开发手册中强制要求pojo类属性使用包装类。
int i = 0; Integer i = null;
3.包装类和基本数据类型之间的转换
基本数据类型 --> 包装类
(1)使用构造器
int value = 50; Integer value2 = new Integer(value); //字符串作为参数 Integer value3 = new Integer("50"); Integer aa = new Integer("aa"); //异常java.lang.NumberFormatException
(2)自动装箱
JDK1.5中新增了这个自动装箱拆箱的功能,类型必须匹配。
Integer value4 = 50; Integer value5 = 50.4; //报错
包装类 --> 基本数据类型
(1)调用包装类对象的xxxValue(
)方法
Integer value2 = new Integer(50); int i = value2.intValue(); Double d1 = new Double(50.3); double d2 = d1.doubleValue();
(2)自动拆箱
Integer v1 = new Integer(30); int v2 = v1;
4.和String类型之间的转换
基本数据类型 --> String类型
(1)String类的valueOf()
静态方法
(2)连接符+
int v1 = 50; String str1 = String.valueOf(v1); String str2 = v1+"";
包装类 --> String类型
Integer v1 = new Integer(50); String str1 = v1.toString(); String str2 = v1.toString(v1);
String类型 --> 基本数据类型
(1)包装类构造器new Integer(str)
(2)包装类的parseInt(str)
静态方法
String str1 = "50"; int v1 = new Integer(str1); int v2 = Integer.parseInt(str1);
String类型 --> 包装类
(1)包装类构造器
String str1 = "55"; Integer v1 = new Integer(str1);
总结
5.面试题
题1
Object o1 = true ? new Integer(1) : new Double(2.0); System.out.println(o1);
输出结果为1.0
三目运算符对于包装类的操作,都会先转换成基本类型,然后根据返回值的接收对象是否是包装类再决定是否装箱。
(1)new Integer()
和new Double()
编译自动拆箱为int和double
(2)表达式2和表达式3要求类型一致,int类型自动提升为double
(3)自动装箱为Double,赋值给o1
(4)打印时,执行子类Double的toString()
方法(多态)
题2
Integer i = new Integer(1); Integer j = new Integer(1); System.out.println(i == j);//false Integer m = 1; Integer n = 1; System.out.println(m == n);//true Integer x = 128; Integer y = 128; System.out.println(x == y);//false
new出来的对象地址肯定不同,结果为false。
Integer类有一个静态内部类,里面定义了一个cache数组存放[-128,127],在这个范围内直接使用,超出则去new。
点击查看代码:Integer静态内部类
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) { 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. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} }
分类:
Java / JAVA基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本