java学习初探十之包装类

1java中八种基本数据类型对应的包装类
基本数据类型 包装类型
byte - java.lang.Byte
short - java.lang.Short
int - java.lang.Integer
long - java.lang.Long

float - java.lang.Float
double - java.lang.Double

boolean - java.lang.Boolean

char - java.lang.Character

思考:java中提供的八种基本数据类型不够用吗?为什么java中还要提供对应的包装类呢?
方便。
举例说明:

package javalang;
public class IntegerTest01 {
    public static void main(String[] args) {
        //基本数据类型
        byte b=10;
        //引用类型
        Byte b1=new Byte(b);
        m1(b1);
    }
    //需求:规定m1方法可以接收java中任何一种数据类型
    //m1方法如果想接收byte类型的数据,可以将byte类型先包装成java.lang.Byte再传递参数
    public static void m1(Object o) {
        System.out.println(o);  
    }
}

2.包装类继承关系
这里写图片描述

3.Integer
(1)以Interger类型为例,学习八种类型

package javalang;

public class IntegerTest02 {
    public static void main(String[] args) {
        //获取int类型的最大值最小值
        System.out.println("int最小值:"+Integer.MIN_VALUE);
        System.out.println("int最大值:"+Integer.MAX_VALUE);

        //以int推byte
        System.out.println("int最小值:"+Byte.MIN_VALUE);
        System.out.println("int最大值:"+Byte.MAX_VALUE);

        //创建Integer类型的对象
        Integer i1=new Integer(10);
        Integer i2=new Integer("123");//String转Integer
        System.out.println(i1);//10
        System.out.println(i2);//124

        //以下程序编译可以通过,运行会报异常,java.lang.NumberFormatException:
        //Integer i3=new Integer("abc");
        //System.out.println(i3);//数字格式化异常,java.lang.NumberFormatException:

        //基本数据类型--》引用类型
        Integer i4=new Integer(10);
        Integer i6=Integer.valueOf(10);
        Integer i7=Integer.valueOf("34");
        //引用类型--》基本数据类型
        int i5=i4.intValue();
        System.out.println(i4+1);//11
        System.out.println(i5+1);//11

        //重要:static int parseInt(String s) String-->int
        int age=Integer.parseInt("25");
        System.out.println(age+1);//26

        //重要 static double parseDouble(String s) String-->Double
        double price=Double.parseDouble("2.4");
        System.out.println(price+1);//3.4

        //将int的十进制转2进制
        String s1=Integer.toBinaryString(10);
        System.out.println(s1);//1010
        //将int的十进制转16进制
        String s2=Integer.toHexString(10);
        System.out.println(s2);//a
        //将int的十进制转8进制
        String s3=Integer.toOctalString(10);
        System.out.println(s3);//12 
    }
}

(2)Integer int String 三种类型转换

package javalang;

public class IntegerTest04 {
    public static void main(String[] args) {
        //1.int-->Integer
        Integer i1=Integer.valueOf(10);
        //2.Integer-->int
        int i2=i1.intValue();

        //3.String-->Intege r
        Integer i3=Integer.valueOf("23");
        //4.Integer-->String
        String s1=i3.toString();

        //5.String-->int
        int i4=Integer.parseInt("123");
        //6.int-->String
        String s2=i4+"";
    }
}

4.自动装箱,自动拆箱
jdk5.0新特性。
以下特性适合jdk1.5及其以后的,jdk1.4及其以下所有版本不能使用以下特性。
(1)自动装箱(auto_boxing),自动拆箱(auto_unboxing)是程序编译期的概念,和程序运行无关
(2)之所以引入是为了方便程序员编码

package javalang;

public class IntegerTest05 {

    public static void main(String[] args) {
        //jdk5.0之前
        //int-->Integer(装箱)
        Integer i1=new Integer(10);
        //Integer -->int (拆箱)
        int i2=i1.intValue();

        //jdk5.0之后,包括5.0
        Integer i3=10;//自动装箱
        int i4=i3;//自动封箱
        System.out.println(i3);//10
        System.out.println(i4+1);//11
    }

}

深入自动装箱,自动拆箱,
以下例子,面试时经常会被问到

package javalang;

public class IntegerTest06 {

    public static void main(String[] args) {
        Integer i1=new Integer(10);
        Integer i2=new Integer(10);

        //这里不会有自动拆箱
        System.out.println(i1==i2);//false

        //比较两个Integer类型的数据是否相等,不能用==
        //Integer重写了Object中的equals方法。
        System.out.println(i1.equals(i2));//true

        //重点
        Integer i3=128;
        Integer i4=128;
        //上面的等同于
        //Integer i3=new Integer(128);
        //Integer i4=new Integer(128);
        System.out.println(i3==i4);//false

        //注意以下程序:
        //如果数据在(-128到127)范围内,java中引入了一个“整形常量池”,在方法中。
        Integer i5=127;
        Integer i6=127;
        System.out.println(i5==i6);//true

        Integer i7=-128;
        Integer i8=-128;
        System.out.println(i7==i8);//true

        Integer i9=-129;
        Integer i10=-129;
        System.out.println(i9==i10);//false 
    }
}

原因也可以从java.lang.Integer源码中看出


public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值