| Author |
boolean question
|
bhadule bhadule
Greenhorn
Joined: Sep 11, 2005
Posts: 10
|
|
Hello Ranchies, can anybody plz explain me the concept of == operator . i got confused...for bellow code my answer was false,true,true.... but it is returning true,true,true..... i think valueOf() returns the instance of invoking wrapper type... plz help me?... public class Test5{ public static void main(String[] args){ Boolean b1 = Boolean.valueOf(true); Boolean b2 = Boolean.valueOf(true); Boolean b3 = Boolean.valueOf("TrUe"); Boolean b4 = Boolean.valueOf("tRuE"); System.out.print((b1==b2) + ","); System.out.print((b1.booleanValue()==b2.booleanValue()) + ","); System.out.println(b3.equals(b4)); } } Regards Hrushi
|
 |
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
Hi, valueOf() method returns the Boolean instance representing the parameter...if the object already exists.. this instance is returned..A new one is not created.. You can have look at the API.. Put this line and execute... System.out.println((b3==b4)==(b1==b2));
|
 |
Amin Mohammed-Coleman
Greenhorn
Joined: Oct 18, 2004
Posts: 15
|
|
The == operator is normally used to compare object references, that is, if 2 references are pointing to the same object on the object heap. So the first result is false because they are not referring to the same object. Whereas the others are true because the contents are the same. The equals method looks at content rather than object reference.
|
 |
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
Hi Amin,
So the first result is false because they are not referring to the same object
The result is true,true,true. And not false true true...  [ October 27, 2005: Message edited by: A Kumar ]
|
 |
 |
|
|
subject: boolean question
|
|
|