java学习日记20230403-包装类
包装类
- 针对八种数据类型相应的引用类型;
- 有了类的特点,就可以调用类的方法
-
boolean Boolean char Char
byte Byte short Short long Long int Integer float Float double Double - 包装类和基本数据类型的转换
- jdk5前手动拆箱和装箱,装箱:基本类型-包装类型
- jdk5后,自动装箱和拆箱
- 自动装箱底层调用的是valueof方法
- 查看代码
public class Wrapper01 { public static void main(String[] args) { int n1 =3; //自动装箱 Integer.ValueOf(n1); Integer integer = n1; //自动拆箱 底层使用是intValue(); int n2 = integer; System.out.println(integer.compareTo(n1)); } }
- 三元运算符要看为一个整体
- true?new Integer(1):new Double(2.0). -----返回1.0 而不是1
- 包装类和String互相转化
- 查看代码
public class WrapperToString { public static void main(String[] args) { Integer integer = 100; //方式1 String string1 = integer +""; //方式2 String s = integer.toString(); //方式3 String s1 = String.valueOf(integer); //String - 包装类 String str = "123"; Integer i = Integer.parseInt(str); Integer integer1 = new Integer(str); } }
创建Interger类型的对象时,若数字在-128-127范围内,则变量直接指向常量池,若超出范围,则在堆内存中new一个对象
只要有基本数据类型,==判断的是值相等
- 查看代码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器