The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes printf string formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "printf string formatting" Watch "printf string formatting" New topic

printf string formatting

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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)

Henry Wong

Joined: Sep 28, 2004
Posts: 20521

It's all explained, in the JavaDoc of the java.util.Formatter class, which is the class that is used to format the text.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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

Joined: Oct 14, 2005
Posts: 19973

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.
subject: printf string formatting
It's not a secret anymore!