StringFormat for Java Boolean Operator

前端 未结 6 465
再見小時候
再見小時候 2020-12-29 17:54

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.

相关标签:
6条回答
  • 2020-12-29 18:35

    '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

    0 讨论(0)
  • 2020-12-29 18:36

    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".

    0 讨论(0)
  • 2020-12-29 18:40

    One more way is -

        String output = String.format("boolean variable is %b",true);
        System.out.print(output); 
    
    0 讨论(0)
  • 2020-12-29 18:41

    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

    0 讨论(0)
  • 2020-12-29 18:42
    System.out.printf("boolean variable is %b",boolVar);
    
    0 讨论(0)
  • 2020-12-29 18:51

    The placeholder for boolean is %b

    0 讨论(0)
提交回复
热议问题