Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
下面哪个能得到true的结果:
A b1 == b2
B b1.equals(b2)
C b1&b2
D b1 | b2
E b1 && b2
F b1 || b2
[解答]:除了A,其他的都是true
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:false
b1.equals(b2):true
b1 & b2:true
b1 | b2:true
b1 && b2:true
b1 || b2:true