Java基础-Integer注意事项以及面试题


先来点基础的知识

                  基本类型和包装类的对应
                byte             Byte
                short            Short
                int              Integer
                long             Long
                float            Float
                double            Double
                char             Character
                boolean           Boolean

上述的八中基本数据类型的对应关系只有 int->Integer    char->Character    两个变化较大,其余都只是将首字母转换为小写。

再来了解一下JDK5的新特性:自动装箱和拆箱

自动装箱:把基本类型转换为包装类类型

自动拆箱:把包装类类型转换为基本类型

public class Demo_Integer {
	public static void main(String[] args) {
//JDK1.5之前
		int a = 100;
		Integer a1 = new Integer(a);	//将基本数据类型包装成对象,装箱
		int b = a1.intValue();		//将对象转换为基本数据类型,拆箱
//JDK1.5之后
		int x = 100;
		Integer x1 = x;			//自动装箱,把基本数据类型转换为对象
		int y = x1 + x;			//自动拆箱,把对象转换为基本数据类型
	}
}

注意事项

public class Demo_Integer {
	public static void main(String[] args) {
		Integer a = null;
		int b = a + 100;	//自动拆箱底层将会调用a.intValue(),a为null,自然会抛出 NullPointerException
		System.out.println(b);
	}
}

面试题

public class Demo_Integer {
	public static void main(String[] args) {
		Integer i1 = new Integer(97);
		Integer i2 = new Integer(97);
		System.out.println(i1 == i2);
		System.out.println(i1.equals(i2));
		System.out.println("-----------");
	
		Integer i3 = new Integer(197);
		Integer i4 = new Integer(197);
		System.out.println(i3 == i4);
		System.out.println(i3.equals(i4));
		System.out.println("-----------");
	}
}

Output:
	false
	true
	-----------
	false
	true
	-----------
原因:
	new 是在堆内存开辟空间的,自然比较地址值(==)都为false.
	由于Integer重写了equals方法,所以equals输出都为true.

你可能感觉太简单了,没有任何技术含量,因为上面的不是重点,看下面代码

public class Demo_Integer {
    public static void main(String[] args) {
        Integer i1 = 127;
        Integer i2 = 127;
        System.out.println(i1 == i2);
        System.out.println(i1.equals(i2));
        System.out.println("-----------");

        Integer i3 = 128;
        Integer i4 = 128;
        System.out.println(i3 == i4);
        System.out.println(i3.equals(i4));
        System.out.println("-----------");
    }
}

Output:
true
true
-----------
false
true
-----------
原因:
	为什么当int大于127就是两个对象,127这个数字是不是觉得很熟悉?
	-128到127是byte的取值范围,如果在这个取值范围内,自动装箱就不会创建新对象了,而从常量池中获取
	超过了byte的取值范围就会在创建新对象

自动装箱其底层会调用valueOf()方法,简单源码分析(JDK1.8):

public final class Integer extends Number implements Comparable<Integer> {
    
    public static Integer valueOf(int i) {
    	//当 i >= -128  且  i <= 127 时,会直接将取缓冲区中的对象
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);//超过了byte取值范围会在堆内存创建
    }

   //内部类充当缓冲区
    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() {}
    }
    
}

8种基本类型的包装类和对象池

java中基本类型的包装类的大部分都实现了常量池技术,这些类是Byte,Short,Integer,Long,Character,Boolean,另外两种浮点数类型的包装类则没有实现。另外Byte,Short,Integer,Long,Character5种整型的包装类也只是在对应值小于等于127时才可使用对象池,也即对象不负责创建和管理大于127的这些类的对象

扩展知识

在jvm规范中,每个类型都有自己的常量池。常量池是某类型所用常量的一个有序集合,包括直接常量(基本类型,String)和对其他类型、字段、方法的符号引用。之所以是符号引用而不是像c语言那样,编译时直接指定其他类型,是因为java是动态绑定的,只有在运行时根据某些规则才能确定具体依赖的类型实例,这正是java实现多态的基础。

JVM中,类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括:加载、验证、准备、解析、初始化、使用和卸载7个阶段。而解析阶段即是虚拟机将常量池内的符号引用替换为直接引用的过程。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值