• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

wrapper class doubt

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so this work specially for boolean values and not for others...?

i mean boolean is spl case for this...
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String literals also work similarly..
String s1 = "123";
String s2 = "123";
s1 == s2 returns true..
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
It's a tiny ad only because the water is so cold.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic