一、八种包装类
1、为什么需要基本数据类型的包装类
有以下需求:方法参数为Object obj,但是有需要传进去一个数字或者字符,
| public class IntegerTest01 { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| |
| MyInt myInt = new MyInt(100); |
| |
| doSome(myInt); |
| } |
| |
| public static void doSome(Object obj){ |
| System.out.println(obj); |
| } |
| } |
2、八种包装类型
| byte java.lang.Byte |
| short java.lang.Short |
| int java.lang.Integer |
| long java.lang.Long |
| float java.lang.Float |
| double java.lang.Double |
| boolean java.lang.Boolean |
| char java.lang.Character |
二、Integer(其他的包装类和Integer用法一样)
1、Integer类的构造方法
| |
| |
| public class IntegerTest03 { |
| public static void main(String[] args) { |
| |
| |
| Integer x = new Integer(100); |
| System.out.println(x); |
| |
| |
| Integer y = new Integer("123"); |
| System.out.println(y); |
| |
| |
| Double d = new Double(1.23); |
| System.out.println(d); |
| |
| |
| Double e = new Double("3.14"); |
| System.out.println(e); |
| |
| Integer a = new Integer("中文"); |
| } |
| } |
2、访问包装类的常量获取最大值最小值
| System.out.println("int的最大值:" + Integer.MAX_VALUE); |
| System.out.println("int的最小值:" + Integer.MIN_VALUE); |
| System.out.println("byte的最大值:" + Byte.MAX_VALUE); |
| System.out.println("byte的最小值:" + Byte.MIN_VALUE); |
3、Integer类常用的方法
| |
| Integer x = new Integer(1000); |
| |
| |
| int y = x.intValue(); |
| System.out.println(y); |
| |
| |
| int retValue = Integer.parseInt("123"); |
| |
| System.out.println(retValue + 100); |
| |
| |
| double value2 = Double.parseDouble("3.14"); |
| System.out.println(value2 + 1); |
| |
| |
| String binaryString = Integer.toBinaryString(3); |
| System.out.println(binaryString); |
| |
| |
| |
| String hexString = Integer.toHexString(16); |
| System.out.println(hexString); |
| |
| |
| |
| String octalString = Integer.toOctalString(8); |
| System.out.println(octalString); |
| |
| |
| |
| Integer i1 = Integer.valueOf(100); |
| System.out.println(i1); |
| |
| |
| |
| Integer i2 = Integer.valueOf("100"); |
| System.out.println(i2); |
三、装箱、拆箱
1、装箱、拆箱
装箱:基本数据类型--转换为-->引用数据类型
拆箱:引用数据类型--转换为-->基本数据类型
| public class IntegerTest02 { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| Integer i = new Integer(123); |
| |
| |
| |
| float f = i.floatValue(); |
| System.out.println(f); |
| |
| |
| |
| int retValue = i.intValue(); |
| System.out.println(retValue); |
| } |
| } |
| |
| Number类中有这样的方法: |
| byte byteValue() 以 byte 形式返回指定的数值 |
| abstract double doubleValue()以 double 形式返回指定的数值 |
| abstract float floatValue()以 float 形式返回指定的数值 |
| abstract int intValue()以 int 形式返回指定的数值 |
| abstract long longValue()以 long 形式返回指定的数值 |
| short shortValue()以 short 形式返回指定的数值 |
| |
2、自动装箱、自动拆箱
jdk1.5之后有了自动装箱和自动拆箱
自动装箱:基本数据类型自动转换成包装类
自动拆箱:包装类自动转换成基本数据类型
| |
| |
| Integer i = 900; |
| |
| |
| |
| int x = i; |
| |
| |
| Integer y = 100; |
| |
| System.out.println(y + 1); |
3、注意
| Integer a = 128; |
| Integer b = 128; |
| System.out.println(a == b); |
| |
| Integer x = 127; |
| Integer y = 127; |
| System.out.println(x == y); |
| |
| |
| java中为了提高程序的执行效率,将[-128到127]之间所有的包装对象提前创建好, |
| 放到了一个方法区的“整数型常量池”当中了,目的是只要用这个区间的数据不需要 |
| 再new了,直接从整数型常量池当中取出来 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)