Java 面试题分析
分析代码如下:
demo1:
public static void main(String[] args)
{
String str1 = "abc";//常量池
String str2 = "abc";//常量池
String str3 = new String("abc");//堆 中
System.out.println(str1 == str2);//同一个直 true
System.out.println(str1 == str3);//不同 false
System.out.println(str1.equals(str3));//Hashcode 相同
System.out.println(str2.equals(str3));//Hashcode 相同
System.out.println();
Integer i =127,j=127,m=129,n=129;
System.out.println(i==j);//
System.out.println(m==n);
}
结果:
true false true true true false
Integer 原码如下:
assert IntegerCache.high >= 127;
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
demo2;
public class NULL
{
public static void test()
{
System.out.println("test");
}
public static void main(String[] args)
{
((NULL) null).test();//同 NULL.test();
NULL n = new NULL();
n.test(); //NULL.test();
Object obj = 0.6324;
System.out.println(obj.getClass());//Double
final StringBuffer a = new StringBuffer("aaa");
a.append("a");//special
System.out.println(a);
}
}

浙公网安备 33010602011771号