• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

printf

 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if this obvious to everyone, it was not to me so I thought I'd highlight for others.

System.out.printf("%b", 123)

returns true.

printf with a boolean (%b) conversion will only return false if the argument is a boolean with a value of false OR if the argument is null
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that comes from C, as I remember correctly in C, if you say

it means same as

so from this point of view it makes sence , but ... again
java is not C ..
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you do this :
Boolean b = new Boolean("whatever");
b is false
and
("%b",whatever )
is true...
and i ll become crazzy before the day of my test lol
;-)
 
Higgledy Smith
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by levani dvalishvili:
I think that comes from C, as I remember correctly in C, if you say

it means same as

so from this point of view it makes sence , but ... again
java is not C ..



Careful, 123 is a literal int, not a variable. Can a literal integer value be equivilant to true or false?
a boolean variable can only be assigned true or false.
123 is not a legit variable name; boolean 123 = true; //will not compile.
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java , boolean are only true or false. He meant in Cpp, true is 0 and false is 1
and you can make an if(1) like that...
:-)
arno
 
Higgledy Smith
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arno Reper:
In java , boolean are only true or false. He meant in Cpp, true is 0 and false is 1
and you can make an if(1) like that...
:-)
arno



OK. Now I understand where he is coming from.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Internally printf calls format and format inturn uses the Formatter class in java.io package. The way it calculates whether it is true or false is...

* If the argument is a boolean then use String.valueOf( boolean )
* If it is null then print false
* For anything else print true.

These are the rules.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic