包装类详解、Integer和Character类的常用方法。

1.包装类的分类

八种基本数据类型对应的引用类型--包装类

前两种包装类Boolean和Character比较特殊,他们是Object的子类

后六种包装类都是Number的子类

在我理解看来,Number和Character,Boolean属于同一级,他们都会Object的zilei,而Byte,Long,Integer,Float,Double,Short又是Number的子类。这就是包装类的继承关系。

2.包装类和基本数据类型之间的转换

这里介绍两个概念

装箱:基本数据类型->包装类型;拆箱:包装类型->基本数据类型

jdk5之前都是手动拆箱与装箱,jdk5以后就实现了自动拆箱与装箱

以Integer <--> int为例

public class Integer01 {
    public static void main(String[] args) {
        //手动装箱
        // int->Integer
        int n1=10;
        Integer integer = new Integer(n1);
        Integer integer1 = Integer.valueOf(n1);
        //手动拆箱
        // Integer->int
        int i = integer.intValue();


        //自动装箱拆箱
        int n2=20;
        //int->integer
        Integer integer2=n2;
        //底层使用Integer.valueOf(n2)
        //自动拆箱
        int n3=integer2;
        //底层使用integer.intValue()
    }
}

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

以Integer和String为例

public class WrapperVSString {
    public static void main(String[] args) {
        //包装类(Integer)->String
        //方法一
        Integer i=100;
        String str1=i+"";
        //方法二
        String s2=i.toString();
        //方法三
        String str3=String.valueOf(i);
        
        
        //String->Integer
        String str4="12345";
        Integer i2=Integer.parseInt(str4);//返回一个int类型的数据,再进行自动装箱
        Integer i3 = new Integer(str4);//构造器

    }
}

4.Integer类和Charater类的常用方法

public class WrapperMethod {
    public static void main(String[] args) {
        System.out.println(Integer.MIN_VALUE); //返回最小值
        System.out.println(Integer.MAX_VALUE);//返回最大值

        System.out.println(Character.isDigit('a'));//判断是不是数字
        System.out.println(Character.isLetter('a'));//判断是不是字母
        System.out.println(Character.isUpperCase('a'));//判断是不是大写
        System.out.println(Character.isLowerCase('a'));//判断是不是小写

        System.out.println(Character.isWhitespace('a'));//判断是不是空格
        System.out.println(Character.toUpperCase('a'));//转成大写
        System.out.println(Character.toLowerCase('A'));//转成小写

    }
}

(1).Integer的创建

创建Integer对象时可以直接创建对象,或者直接赋值。

在自动装箱中,Integer并不是直接创建对象,要先判断传进来的数是否在源代码指定的数组中,数组的范围为(-128~127),如果传入的数在这个范围中,则直接返回,如果不在指定范围,则重新创建。

在如下代码中,i和j是直接创建的对象,地址不相同,所以i和j并不相等。

在m和n的比较中,自动装箱的底层是Integer.valueOf(1),而1在数组范围中,所以并没有创建对象,而是直接返回。所以m和n指向的地址相同,m==n。

public class WrapperExercise02 {
    public static void main(String[] args) {
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i == j); //False

        Integer m = 1; //底层 Integer.valueOf(1); -> 阅读源码
        Integer n = 1;//底层 Integer.valueOf(1);
        System.out.println(m == n); //T
//所以,这里主要是看范围 -128 ~ 127 就是直接返回
//,否则,就 new Integer(xx);
        Integer x = 128;//底层 Integer.valueOf(1);
        Integer y = 128;//底层 Integer.valueOf(1);
        System.out.println(x == y);//False
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值