java-基本数据类型与包装类

八种基本数据类型以及对应的包装类

数据类型包装类占用字节
byteByte1
shortShort2
intInteger4
longLong8
floatFloat4
doubleDouble8
charChar2
booleanBooleantrue/false

基本数据类型与包装类的区别

  1. 默认值不同;
  • 基本数据类型默认值为:0或false;
  • 包装类默认值为:null;
  1. 存储位置不同;
  • 基本数据类型存储在栈空间;
  • 包装类存储在堆空间;
  1. 使用方式不同
  • 基本数据类型:直接声明使用
  • 包装类:直接声明或使用new分配内存地址

基本数据类型、包装类、String之间相互转换

基本数据类型转换为包装类

  • 举例:Integer integer1=new Integer(10);
    @Test
    public  void test1(){
        int num=10;
        Integer integer1=new Integer(10);
        System.out.println(integer1.toString());

        Float aFloat =new Float("12.3");
        aFloat=new Float(12.4);
        System.out.println(aFloat.toString());

        Boolean b1=new Boolean("dddd");
        System.out.println(b1);
    }

包装类转换为基本数据类型

  • 使用包装类的intValue()等方法,将包装类转换为基本数据类型;
    public  void test2(){
        Integer integer1=new Integer(10);
        int num=integer1.intValue();
        System.out.println(num);

        Float aFloat =new Float("12.3");
        aFloat=new Float(12.4);
        float f=aFloat.floatValue();
        System.out.println(f);

        Boolean b1=new Boolean("dddd");
        boolean b =b1.booleanValue();
        System.out.println(b);
    }
自动装箱与自动拆箱
  • JDK1.5 提供了自动装箱(autoboxing)和自动拆箱(autounboxing)
    功能, 从而实现了包装类和基本数据类型之间的自动转换。
    @Test
    public  void test3(){
        int num=10;
        Integer integer=num;//自动装箱
        System.out.println(integer.toString());

        Integer integer1=new Integer(10);
        int num1=integer1;//自动拆箱
        System.out.println(num1);
    }

基本数据类型、包装类与String的相互转换

基本数据类型、包装类转换为String
  • 连接运算转换
        int num=10;
        //方式1:连接运算
        String str1=num+"";
        System.out.println(str1);
  • 调用String.valueof()方法
    @Test
    public  void test4(){
        int num=10;
        //方式1:连接运算
//        String str1=num+"";
//        System.out.println(str1);
        //方式2:调用valueof方法
        Float f=12.3f;
        String str2=String.valueOf(f);
        System.out.println(str2);
        double d=12.56885;
        System.out.println(String.valueOf(d));

    }
String转换为基本数据类型、包装类
  • 使用包装类的Integer.parseInt()等发放转换;
    @Test
    public  void test5(){
        String str1="4541";
        System.out.println(Integer.parseInt(str1));
    }

int与Integer

  1. new Integer()是生成一个指针指向此对象;int则是直接存储数据值;
  2. 在自动装箱过程中,底层执行了Integer的valueOf()方法,该方法会判断值的大小,如果在-128至127这个范围,就返回缓存里面的值,否则就会new一个Integer对象;
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() {}
    }

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

  1. 当我们使用包装类型进行比较的时候,最好使用intValue()方法;
	@Test
	public void test3() {
		Integer i = new Integer(1);
		Integer j = new Integer(1);
		System.out.println(i == j);//false 引用地址不相等
	
		
		Integer m = 1;
		Integer n = 1;
		System.out.println(m == n);//true 读取缓存

		Integer x = 128;//相当于new了一个Integer对象
		Integer y = 128;//相当于new了一个Integer对象
		System.out.println(x == y);//false
	}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅尝酥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值