Java中的泛型和包装类

拜拜、爱过 提交于 2019-12-17 04:27:42
  • 泛型
  • 包装类

泛型

1、泛型的定义:

public class MyArrayList<E> {
    private E[] array;
    ...
}

<>是泛型的标志
E是类型变量,变量名一般要大写
E在定义时是形参,代表 MyArrayList最终传入的类型,现在还不知道

2、泛型的意义:

public class MyArrayList<T> {
    public T[] elem;
    public int usedSize;
    public MyArrayList() {
        //this.elem = new T[10];
        this.elem = (T[])new Object[10]; //注意要这样写
        this.usedSize = 0;
    }
    public void add(T data) {
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }
    public T get() {
        return this.elem[this.usedSize-1];
    }
}

当我们编写这样的代码:

        MyArrayList<String> myArrayList = new MyArrayList<>();
        myArrayList.add("Java"); 
        myArrayList.add(2); //报错
        String ret = myArrayList.get(); //不用进行强转
 

因此泛型的意义:

   1)可以进行自动类型检查
   2)自动进行类型转换

3、泛型的几个坑:

1)  泛型类型的参数不能是简单类型。一定要是对象的包装类
   

  MyArrayList<int> myArrayList3 = new MyArrayList<>(); //报错


2)不能够new一个泛型类型的数组

        this.elem = (T[])new Object[10]; //注意要这样写
        T[] elem = new T[10]; //错误


3)泛型类型的参数不参与类型的组成

        MyArrayList<String> myArrayList = new MyArrayList<>();
        System.out.println(myArrayList); //MyArrayList@16d3586

4、泛型是如何编译的:

擦除机制,(不是替换)编译的时候把尖括号里面的类型擦除为Object类型。泛型只在编译时期才有作用。

包装类

1、8种基本数据类型对应的包装类:

除了char --> Character 和 int --> Integer,其他都是首字母大写

2、自动装箱/装包,自动拆箱/拆包:

        //int --> Integer 自动装箱
        Integer a = 10;
        //手动装箱
        Integer b = Integer.valueOf(20);
        System.out.println(b); //20
        //Integer --> int 自动拆箱
        int c = a;
        //手动拆箱 还可以是double ...
        int d = a.intValue();
        System.out.println(b); //10

总结:装箱调用Integer.valueOf()方法(静态的)、拆箱调用Integer.intValue)方法(可变的)

3、辨析Integer:

        Integer a = 100;
        Integer b = 100;
        System.out.println(a == b);  //true
        Integer a = 150;
        Integer b = 150;
        System.out.println(a == b); //false

Wow......

Integer是引用类型,等号比较的是地址。在代码中进行了自动装箱的过程,因此调用了valueOf()方法。查看valueOf底层代码:如果一个数的值在-128-127之间,每次去缓存区去取。不在的话,没有都会去new一个新的对象。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!