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源碼:java

//緩存基本數據類型代碼
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"這個字符串的引用,則將這個對象的引用加入常量池,返回則個對象的引用;express

  當常量池中存在"mxh"這個字符串的引用,返回這個對象的引用.緩存

  "mxh".intern()在常量池中建立"mxh"的引用,mxh0引用了常量池中的"mxh",故結果爲true.this

 

包裝類型與基本數據類型的區別:code

  1. 包裝類型容許爲null,基本數據類型不能夠;orm

  2. 包裝類型存放在堆中,基本數據類型存放在棧中;對象

  3. 從效率上將,基本數據類型效率高於包裝類型;blog

  4. 包裝類型能夠作泛型,基本數據類型不能夠;內存

 

問題解析:

  1.爲何包裝類型容許爲null,而基本數據類型不能夠?

  基本數據類型存放在棧內存中,包裝類型及對象存放在對內存中,棧內存中存放的是對象的引用地址,包裝類型能夠沒有指向堆的內存地址,即對象能夠爲null;

  2.爲何效率上基本數據類型比包裝類型效率高?

       基本數據類型存放在棧中,而包裝類型存放在堆中,在棧中找到內存地址後還須要去堆中取值,多了一步,因此基本數據類型效率高;

       3.爲何包裝類型能夠作泛型而基本數據類型不能夠?

       集合中存放的都是Object類型及Object的子類,基本數據類型不是Object的子類,固然基本數據類型有對應的包裝類;

相關文章
相關標籤/搜索