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一个对象
只要有基本数据类型,==判断的是值相等
- 查看代码

浙公网安备 33010602011771号