java 包装类

包装类

一、 简介

  • 基本数据类型(8种),不是对象,提供了包装类,将基本数据类型封装成对象来操作
  • 基本数据类型能和包装类型之间相互转换(除去boolean),和String转换
  • 包装类中还包括了基本数据类型相关的属性和其他方法

Number抽象类学习

//获取 int 类型的值
public abstract int intValue();

//获取 long 类型的值
public abstract long longValue();

public abstract float floatValue();

public abstract double doubleValue();

public byte byteValue() {
    return (byte)intValue();
}

public short shortValue() {
    return (short)intValue();
}

二、基本数据类型和包装类

基本类型默认值包装类型包装类型默认值
int0Integernull
long0Longnull
booleanfalseBooleannull
byte0Bytenull
short0Shortnull
float0.0Floatnull
double0.0Doublenull
char‘’Characternull

三、常用方法

  1. 获取最大值 MAX_VALUE,最小值MIN_VALUE

  2. 转换:

    自动拆箱:将包装类转换成基本数据类型

    自动封箱:将基本数据类型转为包装类

    //自动装箱
    Integer integer3 = 10;
    Integer integer3 = Integer.valueOf(10);
    //自动拆箱
    int intValue = integer3;
    int intValue = integer3.intValue();
    
    • 将字符串转为包装类,调用**valueOf()**方法
    • 将包装类转成基本数据类型(自动拆箱)**xxxValue()**方法
    • 将字符串转成基本数据类型,**parseXXX()**方法
    • 将包装类转为字符串,toString()String.valueOf()
  3. compare( ) 比较方法:相同为0,前边小为负,前边大为正

  4. toBinaryString( ) 二进制转换

  5. Integer 有缓存处理 IntegerCache,-128到127之间,在此范围内,使用==判断,会得到true,超过此范围,返回为false,应该使用equals来判断值是否相等

  6. Character相关方法

    char c='a';
    c=Character.toUpperCase(c);//转成大写字母
    c=Character.toLowerCase(c);//转成小写字母
    boolean lowerCase = Character.isLowerCase(c);//是否是一个小写字母
    System.out.println("lowerCase = " + lowerCase);
    boolean upperCase = Character.isUpperCase(c);//是否是一个大写字母
    System.out.println("upperCase = " + upperCase);
    boolean letter = Character.isLetter(c);//判断是否是字母
    System.out.println("letter = " + letter);
    boolean digit = Character.isDigit(c);//判断是否是数字
    System.out.println("digit = " + digit);
    char c1=' ';
    boolean spaceChar = Character.isSpaceChar(c1);//判断是否是空格
    System.out.println("spaceChar = " + spaceChar);
    char[] chars={'A','中'};
    int i = Character.codePointAt(chars, 1);
    System.out.println("i = " + i);
    

拓展内容(面试内容,面试前再回头来听)

  1. 缓存设计的优点

    • 节约空间开销
    • 超过缓存范围才重新分配空间
  2. Integer 类型和 int 类型不是同一个类型

    • 通过方法的重载证明
  3. 两个方法分别定义 int a = 100000;当int 类型的值,超过 short 的最大值时,存储到常量池里面

    public void eat(Integer  integer){
        int a = 0x186a0;
    }
    public void eat(int  i){
        int a = 0x186a0;
    }
    
  4. 开发过程中建议使用包装类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小郑在努力ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值