1.根据下面的程序代码,哪些选项的值返回true?
public class Square { long width; public Square(long l) { width = l; } public static void main(String arg[]) { Square a, b, c; a = new Square(42L); b = new Square(42L); c = b; long s = 42L; } }a == bs == ab == ca.equals(s)答案解析:这题考的是引用和内存。//声明了3个Square类型的变量a, b, c//在stack中分配3个内存,名字为a, b, cSquare a, b, c; //在heap中分配了一块新内存,里边包含自己的成员变量width值为48L,然后stack中的a指向这块内存a = new Square(42L); //在heap中分配了一块新内存,其中包含自己的成员变量width值为48L,然后stack中的b指向这块内存b = new Square(42L); //stack中的c也指向b所指向的内存c = b;//在stack中分配了一块内存,值为42long s = 42L; 如图所示:来看4个选项:A: a == b由图可以看出a和b指向的不是同一个引用,故A错B:s == a一个Square类型不能与一个long型比较,编译就错误,故B错c:b == c由图可以看出b和c指向的是同一个引用,故C正确d:a equal s程序会把s封装成一个Long类型,由于Square没有重写Object的equals方法, 所以调用的是Object类的equals方法,源码如下
public boolean equals(Object obj) { return (this == obj); }其实就是判断两个引用是否相等,故D也错误。2.在jdk1.5的环境下,有如下4条语句:
Integer i01 = 59;int i02 = 59;Integer i03 =Integer.valueOf(59);Integer i04 = new Integer(59)。以下输出结果为false的是:System.out.PRintln(i01== i02);System.out.println(i01== i03);System.out.println(i03== i04);System.out.println(i02== i04);答案解析:Integer i01=59 的时候,会调用 Integer 的 valueOf 方法,
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); }这个方法就是返回一个 Integer 对象,只是在返回之前,看作了一个判断,判断当前 i 的值是否在 [-128,127] 区别,且 IntegerCache 中是否存在此对象,如果存在,则直接返回引用,否则,创建一个新的对象。
在这里的话,因为程序初次运行,没有 59 ,所以,直接创建了一个新的对象。
int i02=59 ,这是一个基本类型,存储在栈中。
Integer i03 =Integer.valueOf(59); 因为 IntegerCache 中已经存在此对象,所以,直接返回引用。
Integer i04 = new Integer(59) ;直接创建一个新的对象。
System. out .println(i01== i02); i01 是 Integer 对象, i02 是 int ,这里比较的不是地址,而是值。 Integer 会自动拆箱成 int ,然后进行值的比较。所以,为真。
System. out .println(i01== i03); 因为 i03 返回的是 i01 的引用,所以,为真。
System. out .println(i03==i04); 因为 i04 是重新创建的对象,所以 i03,i04 是指向不同的对象,因此比较结果为假。
System. out .println(i02== i04); 因为 i02 是基本类型,所以此时 i04 会自动拆箱,进行值比较,所以,结果为真。
新闻热点
疑难解答