Java包装类

包装类

  1. 针对八种基本数据而理性相应的引用类型---包装类
  2. 有了类的特点,就可以调用类中的方法
基本数据类型 包装类
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double



包装类和基本数据类型的转换

基本数据类型和包装类的相互转换

  1. jdk5前的手动装箱和拆箱方式,装箱:基本类型--->包装类型,反之,拆箱
  2. jdk5(包含jdk5)以后的自动装箱和拆箱方式
  3. 自动装箱底层调用的是valueOf方法,比如Integer.valueOf()
java 复制代码
package com.shedu.wrapper_;


public class Integer01 {
    public static void main(String[] args) {
        //演示int《--》Integer的装箱和拆箱
        //jdk5前是手动装箱和拆箱
        //手动装箱
        int n1 = 100;
        Integer integer = new Integer(n1);
        Integer integer1 = Integer.valueOf(n1);

        //手动拆箱
        //Integer ------》int
        int i = integer.intValue();


        //jdk5后,就可以自动装箱和自动拆箱
        int n2 = 200;
        //自动装箱 int---->Integer
        Integer integer2 = n2;//底层使用的是:integer.valueOf(n2)

        //自动拆箱Integer------>int
        int n3= integer2;//底层使用的仍然是intValue()方法
    }
}

包装类型和String类型的相互转换

java 复制代码
package com.shedu.wrapper_;


public class WrapperVSString {
    public static void main(String[] args) {
        //包装类(Integer)-->String
        Integer i = 100;//自动装箱
        //方式1
        String s1 = i+"";
        //方式2
        String s2 = i.toString();
        //方式3
        String s3 = String.valueOf(i);



        //String --> 包装类(Integer)
        String s4 = "123";
        Integer i1 = Integer.parseInt(s4); // 使用到自动装箱
        Integer i2 = new Integer(s4);

    }
}

包装类的常用方法

练习

java 复制代码
package com.shedu.wrapper_;
public class WrapperExercise02 {
    public static void main(String[] args) {
        methods();
    }

    public static void methods() {
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i == j);//false


        /*
        源码:
        1. 如果i在IntegerCache.low(-128)~IntegerCache.high(127)范围内,之际从数组中返回
        2.如果不在这个范围内,就直接new Integer()
        public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
         */
        //当范围在-126 ------127之间时,直接返回值
        Integer m = 1;//底层调用Integer.valueOf();
        Integer n = 1;//底层调用Integer.valueOf();
        System.out.println(m == n); //true


        //当范围在-126 ------127之间时,直接返回值
        //否则,就new Integer();
        Integer x = 128;
        Integer y = 128;
        System.out.println(x == y);//false
        //当有基本数据类型进行比较,比较的就是数值
        int n11 = 128;
        System.out.println(n11 == x);//true
    }
}
相关推荐
青山木2 分钟前
Hot 100 --- 跳跃游戏 II
java·数据结构·算法·leetcode·贪心算法
ly76894 分钟前
Spring 中的 @Configuration 与 @Component 差异:为何代理时机决定 Bean 生命周期行为
java·后端·spring·注解·代理·bean生命周期
qq_4523962314 分钟前
第十二篇:《数据采集:Grafana Alloy、Fluent Bit、Vector 的选型与配置》
java·贪心算法·grafana
疯狂打码的少年15 分钟前
【计算机网络】OSI/RM七层模型(层次结构、各层功能速览)
开发语言·笔记·计算机网络·php
小灰灰搞电子16 分钟前
Rust+Slint 实现ModbusRTU从机调试助手源码分享
开发语言·rust·modbusrtu
程序喵大人17 分钟前
【C++入门】值类别与表达式 - 03 引用绑定:为什么有些参数能接住临时对象
开发语言·c++·引用绑定
珍珠先生24 分钟前
P16 · IDEA 启动报错:找不到或无法加载主类
java
子非鱼a27 分钟前
【WEB】[RoarCTF 2019]Easy Java
java·开发语言
西峰u27 分钟前
电商项目测试报告
java·项目测试报告
软件黑马王子32 分钟前
19.资源加载模块:主要作用和基本原理
开发语言·前端框架·c#