• 包装类


    java.lang

    基本数据类型,没有对应的方法对这些数据进行操作,使用包装类,可以对这些基本数据类型和对象一样进行操作

    基本类型 对应包装类
    int Integer
    char Character
    byte Byte
    double Double
    short Short
    long Long
    float Float
    boolean Boolean

    除了前两个,其他的都是首字母大写

    装箱:把基本类型的数据转换为包装类

    // 构造方法:
    // Integer(int valur):构造一个Integer对象
    // Integer(String str): 构造一个Integer对象,它代表字符串str所指示的int值,比如str = “100”。(str = “a” 会抛出异常)
    
    // 静态方法:
    // static Integer valueOf(int i):返回一个表示指定参数int值的Integer对象 
    // static Integer valueOf(String str): 返回指定的参数字符串str代表的Integer对象
    Integer int1 = Integer.valueOf(100);
    Integer int2 = Integer.valueOf("100");// Integer int2 = Integer.valueOf("aba");这种字符串会抛出异常
    

    拆箱:包装类转换为基本数据类型

    // 成员方法:
    int intValue():以int类型返回Integer对象的值
    

    自动装箱:

    Integer int1 = 1;
    // 这句话就相当于Integer int1 = new Integer(1)
    

    自动拆箱:

    Integer int1 = 1;
    int1 = int1 + 2;// 一个包装类直接+2
    
    ArrayList<Integer> list = new ArrayList<>();
    // ArrayList无法直接存储基本数据类型,但是可以存储包装类,在调用add方法的时候,就隐含自动装箱list.add(new Integer(1))
    // 在调用方法get的时候可以拿int类型的变量接收,就隐含了自动拆箱int a = list.get(0).intValue();
    list.add(1);
    int a = list.get(0);
    
    
  • 相关阅读:
    Justoj 2389: 拼接三角形 二分查找
    P4513 小白逛公园(线段树求区间最大子段和)
    勾股数
    费马大定理
    D1. Kirk and a Binary String (easy version)
    Find the median(线段树离散化+区间更新)
    String
    最小表示法
    扩展KMP模板(学习)
    K-th Closest Distance
  • 原文地址:https://www.cnblogs.com/zhuobo/p/10620065.html
Copyright © 2020-2023  润新知