int和integer的区别与比较?

区别:
1.本质上:int是八大基本数据类型之一,integer是int的包装类
2.使用:integer必须是实例化之后才可以使用,int不需要
3.内存调用:当new一个integer的时候,实际上是对象的引用,生成一个指针指向对象,int是直接存储数据的值
4.默认值:integer的默认值是null,int的默认值是0

1、自动装箱
自动装箱其实就是将基本数据类型转换为引用数据类型(对象)

2、自动拆箱
自动拆箱其实就是将引用数据类型转化为基本数据类型

比较:
观看int和integer之间的比较的时候先得了解’=='和‘equals’的区别以及内存地址和值的区别,可以自己整理出equals时两者之间的区别(请看小编其他的博文)

1.不是new生成的integer变量a和通过new生成的integer变量b进行==比较时结果为false

		Integer a=100;
		Integer b=new Integer(100)
		a==b
		
		结果false:a,b内存中的地址不一样,
		a指向常量池中的对象,b指向堆中重新生成的对象

2.不是new生成的integer变量a和new生成的integer变量b两者与int变量c进行==比较是结果为true

        Integer a=100;
        Integer b = new Integer(100);
        int c=100;
        System.out.println(a==c);
        System.out.println(b==c);
        
        结果true:integer包装类与int基本数据类型进行==比较的时候,
        java会自动拆箱,装换成两个int的比较


	int c=100,和integer a=100
	内存比较:实际上是同一种流程:
	先去查找常量池中有没有缓存该数据,
	如果有则直接返回数据引用即可,
	如果没有则需要在常量池中创建该数据之后再返回引用。

	  Integer b = new Integer(100);
	  内存分析:首先查找常量池有没有该数据,
	  如果有则直接返回栈内存进行地址的引用
	  如果没有则首先需要在栈内存中创建该引用(b),在堆内存创建具体对象(new Integer(100),并且需要在常量池中创建该对象,最后返回栈内存的引用
	  ~~详细可查看一个对象实例化的过程~~ 

3.两个通过new生成的integer变量ab进行==比较时结果是false

        Integer a=new Integer(100);
        Integer b = new Integer(100);
        System.out.println(a==b);
        
        结果fasle:生成两个新的对象,内存地址不同

4.非new生成的integer变量ab进行==比较的时候,如果值大小在-128-127之间结果为true,值不在之间为false

//        Integer a=-128;
//        Integer b = -128;
        Integer a=-129;
        Integer b = -129;
        System.out.println(a==b);
        
        结果为fasle:

貌似原因是这样的:当java编译integer a=-128时,先装换成
integer a=integer.valueof(-128),但是呢javaAPI对valueOf的定义是

		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() {}
    }

    /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
    
代码大概的意思好像是说当值在-128-127之间时会进行缓存,
之后有相同的值就直接去缓存中拿,不会重新new对象,
所以说值在-128-127之间就会相等
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值