java装箱拆箱和泛型学习笔记

java装箱拆箱和泛型

装箱和拆箱

装箱
  • 把基本类型转换为包装类型
public static void main(String[] args) {
        int a = 10;
        Integer ii = Integer.valueOf(a);
        Integer ii2 = a;
        System.out.println(a);
        System.out.println(ii);
        System.out.println(ii2);
    }//基本类型转换为应用类型
拆箱
  • 把包装类型转换为基本类型
    public static void main(String[] args) {
        Integer ii = 10;//装箱

        int a = ii;//自动拆箱
        int b = ii.intValue();//手动拆箱
        double c = ii.doubleValue();//转换为不同包装类
    }
装箱中的问题
public static void main(String[] args) {
        Integer a = 100;
        Integer b = 100;
        System.out.println(a == b);//true

        Integer c = 200;
        Integer d = 200;
        System.out.println(c == d);//false
    }//超过128,存储时new新对象

泛型

/*
* @param <T>:代表当前类是一个泛型类
*/
class MyArray<T>{
    public Object[] array = new Object[10];//实例化泛型数组
    public void setValue(int pos,T x){
       array[pos] = x;
    }
    public T getValue(int pos){
        return (T)array[pos];
    }
}
public class test2 {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();//指定当前类型为整型,<>只能写类类型,不能写简单类型
        myArray.setValue(0,1);
        myArray.setValue(1,2);
        //myArray.setValue(2,"hello");错误
        Integer a = myArray.getValue(0);//不用强转
        System.out.println(a);
    }
  • 只在编译阶段存在,运行时没有泛型的概念
  • JVM中没泛型的概念
泛型的边界
class MyAarray<E extends Number>{

}//只接受Number或Number的子类型作为E的类型实参
//定义一个泛型类,找到数组当中最大的值
class Alg<T extends Comparable<T>>{
    public T findMax(T[] array){
        T max = array[0];
        for(int i = 1;i < array.length;i++){
            if (array[i].compareTo(max) > 0){
                max = array[i];
            }
        }
        return max;
    }
}
public class test3 {
    public static void main(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] array = {1,2,3,4,5,6,7,8,9,10};
        Integer x = alg.findMax(array);
        System.out.println(x);
    }
}
泛型方法
  • 朴素方法
class Alg2{
    public <T extends Comparable<T>> T findMax(T[] array){
        T max = array[0];
        for(int i = 1;i < array.length;i++){
            if (array[i].compareTo(max) > 0){
                max = array[i];
            }
        }
        return max;
    }
}
public class test3 {
    public static void main(String[] args) {
        Alg2 alg2 = new Alg2();
        Integer[] array = {1,2,3,4,5,6,7,8,9};
        Integer x = alg2.<Integer>findMax(array);
        System.out.println(x);
    }
} 
  • 静态方法
class Alg3{
    public static <T extends Comparable<T>> T findMax(T[] array){
        T max = array[0];
        for(int i = 1;i < array.length;i++){
            if (array[i].compareTo(max) > 0){
                max = array[i];
            }
        }
        return max;
    }
}
public class test3 {
    public static void main(String[] args) {
        Integer[] array = {1,2,3,4,5,6,7,8,9};
        Integer x = Alg3.<Integer>findMax(array);//不需要对象
        System.out.println(x);
    }
}
  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值