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