StringFormat for Java Boolean Operator

Java

Java Problem Overview


I know its very simple question. but I would like to know the stringformat for boolean operator. For example, below shows the string formats for integer, string and float. what could be for boolean operator true/false?

System.out.printf("The value of the float " +
                  "variable is %f, while " +
                  "the value of the " + 
                  "integer variable is %d, " +
                  "and the string is %s", 
                  floatVar, intVar, stringVar); 

Java Solutions


Solution 1 - Java

'b' or 'B' general 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". java docs : http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

enter image description here

Solution 2 - Java

System.out.printf("boolean variable is %b",boolVar);

Solution 3 - Java

The placeholder for boolean is %b

Solution 4 - Java

One more way is -

    String output = String.format("boolean variable is %b",true);
	System.out.print(output); 

Solution 5 - Java

System.out is a PrintStream and the documentation for PrintStream.printf links to the format stream syntax which has a table of all of the conversions. The first entry in that table:

> 'b', 'B' - 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".

Solution 6 - Java

You can try this

    float floatVar=1.0f;
    int intVar=1;
    String stringVar="hi";
    boolean boolVar=false;
    System.out.printf("The value of the float " +
                    "variable is %f, while " +
                    "the value of the " +
                    "boolean variable is %b, " +
                    "and the string is %s",
            floatVar, boolVar, stringVar);

%b is you are looking at

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUmesh PatilView Question on Stackoverflow
Solution 1 - JavaLMKView Answer on Stackoverflow
Solution 2 - JavabetteroutthaninView Answer on Stackoverflow
Solution 3 - JavaJensView Answer on Stackoverflow
Solution 4 - JavaNinad PingaleView Answer on Stackoverflow
Solution 5 - JavaChris MartinView Answer on Stackoverflow
Solution 6 - JavaRuchira Gayan RanaweeraView Answer on Stackoverflow