| Author |
doubt in boxing
|
Tashi Rautela
Greenhorn
Joined: Jun 27, 2007
Posts: 22
|
|
public class Boxing6 { public static void main(String[] args) { Boolean b1 = new Boolean(true); Boolean b2 = new Boolean(true); boolean b3 = true; Boolean b4 = true; System.out.println(b1==b2); System.out.println(b1==b3); System.out.println(b3 == b4); System.out.println(b1 == b4); } } the answer is false true true false how??? anyone please explain
|
 |
Vassili Vladimir
Ranch Hand
Joined: Mar 08, 2007
Posts: 1585
|
posted

0
|
Hi, The first if statement is checking for the references, and since each object refers to a different object on the heap, you'll get false. The second if statement automatically un-boxes b1 and compares it with b3 which is true, so you'll get true. The third if statement automatically un-boxes b4 (which was automatically boxed) and compares it with b3 which is true, so you'll get true. The fourth statement is also checking for references (because, remember, b4 is an automatic boxing), so you'll get false. Best of luck ...
|
Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
|
 |
Quintin Stephenson
Ranch Hand
Joined: Nov 16, 2006
Posts: 40
|
|
Hi Tashi Read the following forum: http://www.coderanch.com/t/262155/java-programmer-SCJP/certification/equals It deals with the same set of topics (shallow vs. deep comparisons and autoboxing) Cheers Q
|
If at first you don't succeed, try, try again. If you don't try you have failed.
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Thanks,
|
cmbhatt
|
 |
Tashi Rautela
Greenhorn
Joined: Jun 27, 2007
Posts: 22
|
|
thanks to all...understood
|
 |
 |
|
|
subject: doubt in boxing
|
|
|