Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wrapper class doubt

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
shetal bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so this work specially for boolean values and not for others...?

i mean boolean is spl case for this...
 
Jiji Cherian
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals also work similarly..
String s1 = "123";
String s2 = "123";
s1 == s2 returns true..
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic