Java基础面试题--包装类型与基本数据类型(String、Integer)

public class TestInteger {
	
	public static void main(String[] args) {
		//Integer面试题
		Integer n1 = 1;
		Integer m1 = new Integer(1);
		Integer c1 = Integer.valueOf(1);
		
		Integer n2 = 127;
		Integer m2 = new Integer(127);
		Integer c2 = Integer.valueOf(127);
		
		Integer n3 = 128;
		Integer m3 = new Integer(128);
		Integer c3 = Integer.valueOf(128);
		
		System.out.println(n1 == m1); //false
		System.out.println(n1 == c1); //true
		System.out.println(m1 == c1); //false
		
		System.out.println(n2 == m2); //false
		System.out.println(n2 == c2); //true
		System.out.println(m2 == c2); //false
		
		System.out.println(n3 == m3); //false
		System.out.println(n3 == c3); //false
		System.out.println(m3 == c3); //false
		
		//String面试题
		
		final String MESSAGE="taobao";
		
		String a ="tao"+"bao";
	    String b="tao";
	    String c="bao";
	    System.out.println(a==MESSAGE);     //true
	    System.out.println((b+c)==MESSAGE); //false
	   
	}

}
        Integer num1 = new Integer(100);
        Integer num2 = new Integer(100);
        System.out.println(num1 == num2);      //包装类型存放在堆中,内存地址指向不同
        System.out.println(num1.equals(num2)); //只判断值是否相同

        Integer num3 = Integer.valueOf(100);
        System.out.println(num1 == num3);      //false

        Integer num4 = Integer.valueOf(100);
        System.out.println(num4 == num3);      //true

        Integer num5 = Integer.valueOf(200);
        Integer num6 = Integer.valueOf(200);
        System.out.println(num5 == num6);      //false

  

 

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);
}
valueOf(String s)和valueOf(int i)两个方法基本相同,此方法将始终缓存-128到127范围内的值,如果i的值在-128至127之间,则返回基本数据类型,不创建Integer对象,基本数据类型数据保存在JVM栈中,如果不在缓存返回则创建Integer对象.

String  intern方法(返回常量池中该字符串的引用):

     /**
     * Returns a canonical representation for the string object.
     * <p>
     * A pool of strings, initially empty, is maintained privately by the
     * class {@code String}.
     * <p>
     * When the intern method is invoked, if the pool already contains a
     * string equal to this {@code String} object as determined by
     * the {@link #equals(Object)} method, then the string from the pool is
     * returned. Otherwise, this {@code String} object is added to the
     * pool and a reference to this {@code String} object is returned.
     * <p>
     * It follows that for any two strings {@code s} and {@code t},
     * {@code s.intern() == t.intern()} is {@code true}
     * if and only if {@code s.equals(t)} is {@code true}.
     * <p>
     * All literal strings and string-valued constant expressions are
     * interned. String literals are defined in section 3.10.5 of the
     * <cite>The Java™ Language Specification</cite>.
     *
     * @return  a string that has the same contents as this string, but is
     *          guaranteed to be from a pool of unique strings.
     */
    public native String intern();

  

String mxh1 = "mxh".intern();
String mxh0 = "mxh";
System.out.println(mxh0 == mxh1); //true

  当常量池中不存在"mxh"这个字符串的引用,则将这个对象的引用加入常量池,返回则个对象的引用;

  当常量池中存在"mxh"这个字符串的引用,返回这个对象的引用.

  "mxh".intern()在常量池中创建"mxh"的引用,mxh0引用了常量池中的"mxh",故结果为true.

 

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

  1. 包装类型允许为null,基本数据类型不可以;

  2. 包装类型存放在堆中,基本数据类型存放在栈中;

  3. 从效率上将,基本数据类型效率高于包装类型;

  4. 包装类型可以做泛型,基本数据类型不可以;

 

问题解析:

  1.为什么包装类型允许为null,而基本数据类型不可以?

  基本数据类型存放在栈内存中,包装类型及对象存放在对内存中,栈内存中存放的是对象的引用地址,包装类型可以没有指向堆的内存地址,即对象可以为null;

  2.为什么效率上基本数据类型比包装类型效率高?

       基本数据类型存放在栈中,而包装类型存放在堆中,在栈中找到内存地址后还需要去堆中取值,多了一步,所以基本数据类型效率高;

       3.为什么包装类型可以做泛型而基本数据类型不可以?

       集合中存放的都是Object类型及Object的子类,基本数据类型不是Object的子类,当然基本数据类型有对应的包装类;

posted @ 2019-06-15 22:01  尘世间迷茫的小书童  阅读(427)  评论(0编辑  收藏  举报