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源码:

//缓存基本数据类型代码
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.

猜你喜欢

转载自www.cnblogs.com/mxh-java/p/11029026.html
今日推荐