• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Boolean objects and primitives

 
Ranch Hand
Posts: 103
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Namaste Friends !!

Please explain me why in the following statemennt



results in b= false

and

following statement



Prints true ?

Thanks
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings Vishal.


following statement
System.out.printf("%b","nn");

Prints true ?



Here it is just printing the default value as assigned to the formatter .


From the java docs

If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true".

 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The java docs for this constructor explain why b equates to false.


public Boolean(String s)
Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true". Otherwise, allocate a Boolean object representing the value false. Examples:
new Boolean("True") produces a Boolean object that represents true.
new Boolean("yes") produces a Boolean object that represents false.

 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This makes a java object of Boolean class. If the String equals to "true" (not case sensitive), the object contains true value. Otherwise false.


Actually, "%b" or "%B" is not like Boolean class, if check whether the given arg(here "nm") is a Boolean object or boolean. If it is this prints String.valueOf(arg). For other data types this prints "true"...

eg:-
This prints "true" as given arg is a String object...(Not Boolean or boolean)


This prints "false" as given arg is a boolean...(Therefore this call String.valueOf(arg)....)
 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Friends !! thank you for explaining me the concepts..
 
reply
    Bookmark Topic Watch Topic
  • New Topic