包装类的概念和分类

包装类(熟悉)

包装类的概念

  通常情况下基本数据类型的变量不是对象,为了满足万物皆对象的理念就需要对基本数据类型的变
量进行打包封装处理变成对象,而负责将这些变量声明为成员变量进行对象化处理的相关类,叫做包装

如:

  Person p = new Person();

  int num = 10;

p是对象,num从语法层面上看不是对象,如果要把num变成对象应该打包到一个类中:

public class myInt{

  private int num = 10;

}

java官方提供的八个包装类:

   在很多的场合中要求这些变量打包成对象的时候,我们就可以把它打包成对象间接的完成这些功能。

Integer类的概述

  java.lang.Integer类内部包装了一个int类型的变量作为成员变量,主要用于实现对int类型的包装并
提供int类型到String类之间的转换等方法。

下面案例说明:过时的方法也有替代的产品

package com.lagou.task11;

import com.lagou.task10.StaticOuter;

public class IntegerTest {
public static void main(String[] args) {
// 1.打印integer类中常用的常量数值
System.out.println("最大值是:" + Integer.MAX_VALUE);
System.out.println("最小值是:" + Integer.MIN_VALUE);
System.out.println("所表示二进制的位数是: " + Integer.SIZE);
System.out.println("所占字节的个数是:" + Integer.BYTES);
System.out.println("对应int类型的class实例是:" + Integer.TYPE);

System.out.println("-----------------------------------------------");
// 2.使用构造方法来构造Integer类型的对象并打印
Integer it1 = new Integer(123); //已过时的方法会有一条横线
System.out.println("it1 = "+ it1); // 自动调用tostring方法 调用重写以后的方法
Integer it2 = new Integer("456"); //该方法已过时,使用valueOf方法取代,相当于从int类型到integer类型的转换
System.out.println("it2 = " + it2);
// 上述方法已过时,建议使用valueOf()方法取代
Integer it3 = Integer.valueOf(789);

System.out.println("it3 = " + it3);
// parselnt(String s)参数要的时字符串,但是我们要的是int类型,int类型我们可以把他放在valueOf()里面
Integer it4 = Integer.valueOf("456"); //相当于从string类型到integer类型的转换
System.out.println("it4 = "+ it4);
Integer it5 = new Integer(1);
System.out.println(it5);
//获取调用对象的整数数据,相当于从integer类型到int类型的转换
int it6 = it5.intValue();
System.out.println("获取到的整数数据是:" + it6); // 最后拼接完最后都会得到字符串。

}
}
     * @deprecated
     * It is rarely appropriate to use this constructor. The static factory
     * {@link #valueOf(int)} is generally a better choice, as it is
     * likely to yield significantly better space and time performance.
     */
    @Deprecated(since="9")
    public Integer(int value) {
        this.value = value;
    }

在源码中标注:在java9中标记着已过时,虽然这个方法已过时了但是请使用替代的方法valueOf()
     * @deprecated
     * It is rarely appropriate to use this constructor.
     * Use {@link #parseInt(String)} to convert a string to a
     * {@code int} primitive, or use {@link #valueOf(String)}
     * to convert a string to an {@code Integer} object.
     */
    @Deprecated(since="9")
    public Integer(String s) throws NumberFormatException {
        this.value = parseInt(s, 10);
    }
上面标注写的很明白parseint可以得到int类型,int类型又可以放到valueOf()里面

总结:

  1、已过时的方法通常都有替代方案,通常在源码注释中可以看到推荐使用什么方法。

  2、java9以后不能使用构造方法得到integer对象,推荐使用valueOf(int类型对象)或者parselnt(字符串对象)或者用valueOf(传字符串)方法得到integer对象

  3、integer类内部实际上就是被int类型包装,所谓得包装就是拿int类型的变量作为这个类得成员变量,只要new这个类的变量都会得到这个类的成员变量,这样就相当于包装。

  4、intValue这个方法没有static修饰,需要引用点调用该方法,这个方法得到的是一个int类型的数字,所有需要使用int类型保存该数据,当然如果使用字符串与之拼接得到的数据是string类型。

(1)常用的常量

class实例暂时理解为类型的名称

 (2)常用的方法

 (3)装箱和拆箱的概念

  在Java5发布之前使用包装类对象进行运算时,需要较为繁琐的“拆箱”和“装箱”操作;即运算前先将
包装类对象拆分为基本类型数据,运算后再将结果封装成包装类对象。
  从Java5开始增加了自动拆箱和自动装箱的功能。

  装箱:在上述编写的方法中可以实现从int类型到integer类型的转换,integer类实际上是int类的包装类,我们把这种从int类型到integer类型的转换过程,我们把他叫做装箱。什么叫做装箱?装箱的概念就是把箱子。我要想把int类型变成integer类型实际上就需要把箱子打开,integer打开把int类变成integer类的成员变量,这样相当于打包好了,实际上这个过程就叫做装箱。

  拆箱:所有把integer打开,把里面的数据拿出来这个过程叫做拆箱。

  在java5以前我们使用valueOf()、intValue()实现装箱/拆箱,但是我们从java5开始我们增加了自动装箱和自动拆箱的机制。

        // 2.java5以前装箱和拆箱
        Integer it7 = Integer.valueOf(1);
        System.out.println("装箱:" + it7);
        int it8 = it7.intValue();
        System.out.println("拆箱:" + it8);
        // 3.从java5开始增加了自动装箱和自动拆箱的机制
        Integer it9 = 1001;
        System.out.println("自动装箱:"+it9);
        int it10 = it9;
        System.out.println("自动拆箱:"+it10);

(4)自动装箱池

  在Integer类的内部提供了自动装箱池技术,将-128到127之间的整数已经装箱完毕,当程序中使用

该范围之间的整数时,无需装箱直接取用自动装箱池中的对象即可,从而提高效率。

以下考点为什么127比较是true,128比较是false?在Integer类的内部提供了自动装箱池技术,将-128到127之间的整数已经装箱完毕。

        //  笔试考点
        Integer it11 = 127;
        Integer it12 = 127;
        Integer it13 = new Integer(127);
        Integer it14 = new Integer(127);
        System.out.println(it11== it12);        //比较地址 true
        System.out.println(it11.equals(it12));  //比较内容 true
        System.out.println(it13 == it14);       //比较地址  false
        System.out.println(it13.equals(it14));  //比较内容  true

自动装箱池:就是java把该装的数据装在一个水池中,所以调用这个自动装箱池的数据地址是一样的,我们使用装箱池中的数据在范围内比较自然就是true。

  当然我们可以适当的调整自动装箱池,这一块概念就涉及到我们的jvm的调优,在后续的课程中有讲解。

查找cache,可以在源码中找到

 integer类除了可以把字符串转换为int类型,还能实现进制转换:

        //  4.实现静态方法的调用
        int it15 = Integer.parseInt("200");
        //  int it16 = Integer.parseInt("200a");    编译ok,运行发生NumberFormatException数字格式异常,因为有字母
        System.out.println("字符串转换为整数的结果是:"+it5);
        System.out.println("根据参数指定的整数获取对应的十进制字符串是:"+Integer.toString(200));
        System.out.println("根据参数指定的整数获取对应的二进制字符串是:"+Integer.toBinaryString(200));
        System.out.println("根据参数指定的整数获取对应的十六进制字符串是:"+Integer.toHexString(200));
        System.out.println("根据参数指定的整数获取对应的八进制字符串是:"+Integer.toOctalString(200));
原文地址:https://www.cnblogs.com/xujiaqi/p/13817313.html