wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wrapper class doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wrapper class doubt" Watch "wrapper class doubt" New topic
Author

wrapper class doubt

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hii...

in one of DAN question as follow

class D {
public static void main (String[] args) {
Boolean b1 = Boolean.valueOf("trUE"); // 1
Boolean b2 = Boolean.valueOf("Even more true"); // 2
Boolean b3 = Boolean.valueOf(null); // 3
System.out.print((b1==b2) + ",");
System.out.print((b2==b3) + ",");
System.out.println(b3==b1);
}}

What is the result of attempting to compile and run the program?

a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. Compile-time error
j. Run-time error
k. None of the above

the answer is -> false,true,false
with explaination.

but how can the 2nd option be true....
i think the answer should be false,false,false because all new objects are created and allocated to different reference variable which refer to different objects ?

pls explain me regarding DAN answer ??

didn't get it ...


Thanks and Regards, Amit Taneja
shetal bansal
Ranch Hand

Joined: May 09, 2005
Posts: 63
If you try printing objects b1,b2,b3,we get
b1 is true,b2 and b3 are false.
So b2 and b3 are pointing to the same object reference(false in this case) and hence b2==b3 is returning true.
Can anyone add more to this??
Kumar J
Ranch Hand

Joined: Feb 13, 2005
Posts: 35
hi,
see this from api

public static Boolean valueOf(String s)

Returns a Boolean with a value represented by the specified String. The Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Example: Boolean.valueOf("True") returns true.
Example: Boolean.valueOf("yes") returns false.


so the values contained are true and false and false resp.

so u get false,true,false
Got it


With Regards,<br />Kumar J
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
no what you ppl are saying i got that point ...
but what i am asking is that ... when two boolean object refers to different object then how ...simply comparing with == will result the true ...

it will return true when comparing with == when both reference variable pointing to same wrapper object... or any object....

but here b2,b3 and even b1 refers to different object....


now any body pls explain me....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

It's an implemenation detail... Apparently, the Boolean class returns the same reference for equal values. Kinda makes sense as there is only two possible values, so why not create them in advance?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
so this work specially for boolean values and not for others...?

i mean boolean is spl case for this...
Jiji Cherian
Greenhorn

Joined: Apr 15, 2005
Posts: 14
String literals also work similarly..
String s1 = "123";
String s2 = "123";
s1 == s2 returns true..


Rgds.<br /> <a href="mailto:mr.jiji@gmail.com" rel="nofollow">mr.jiji@gmail.com</a>
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
This is a practical example of the fundamental principle that constructors violate encapsulation. Constructors should always be declared private as a matter of good form. Nothing to do with SCJP of course...


Tony Morris
Java Q&A (FAQ, Trivia)
 
 
subject: wrapper class doubt
 
Similar Threads
Boolean wrapper objects
Boolean Wrappers?
Dan's Question : Fundamental Classes
Question from Dans Test
Wrapper Equality test