Java basic interview questions (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 Source:

//缓存基本数据类型代码
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)];
        new new Integer return (i); 
} 
valueOf (String S) and valueOf (int i) two substantially identical method, this method will always cached value within the range of -128 to 127, if the value of i is between -128 to 127 the basic data type is returned, does not create an Integer object, the type of data stored in the data base JVM stack, if not in the cache returns an Integer object is created.

String intern method (returns the string constant pool references):

     /**
     * 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

  When "mxh" string constant pool reference does not exist, then the object reference constant pool added, returns a reference to the object;

  When reference exists "mxh" string constant pool, the returned object.

  "Mxh" .intern () created in the constant pool references "mxh" of, mxh0 cited the constant pool "mxh", so the result is true.

Guess you like

Origin www.cnblogs.com/mxh-java/p/11029026.html