• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

printf string formatting

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following program



The output is



Can someone please explain how a double value (i.e. 123.456) is converted to a boolean (ie. true)



Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
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 all explained, in the JavaDoc of the java.util.Formatter class, which is the class that is used to format the text.

Henry
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i saw the description in the API.

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(arg). Otherwise, the result is "true".


It just seemed weird that any string can result in a value of true. For example, the following expression also prints true.

 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're better off believing what the documentation says, rather than what your intuition says. For one thing that's how the JVM is actually going to work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic