Boolean b1 = new Boolean(true);调试
Boolean b2 = new Boolean(true);对象
下面哪一个能获得true的结果:it
A b1 == b2class
B b1.equals(b2)test
C b1&b2object
D b1 | b2引用
E b1 && b2程序
F b1 || b2demo
[解答]:除了A,其余的都是truemargin
b1,b2两个是对象,两个对象的内容是相同的,可是两个对象所引用的地址是不一样的,因此A不对,
equals()就是用来比较对象的内容的,因此B正确,
CDEF,JAVA中对象能够自动装箱、拆箱成为基本类型,
[程序调试]
package com.sam.test.object;
/**
* 对象的比较
* @author Sam
* @2013-11-7
*/
public class CompileObject {
public CompileObject() {
// TODO Auto-generated constructor stub
}
public void paramInit(String args){
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//CompileObject demo = new CompileObject();
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
System.out.println("b1== b2:"+(b1== b2));
System.out.println("b1.equals(b2):"+b1.equals(b2));
System.out.println("b1 & b2:"+(b1 & b2));
System.out.println("b1 | b2:"+(b1 | b2));
System.out.println("b1 && b2:"+(b1 && b2));
System.out.println("b1 || b2:"+(b1 || b2));
}
}
[控制台结果]
b1== b2:falseb1.equals(b2):trueb1 & b2:trueb1 | b2:trueb1 && b2:trueb1 || b2:true