Close

Java String Formatting - How to format boolean using String#printf()?

Java String Formatting Java 

This example shows how to format boolean expressions with String#printf()

The format specifiers %b or %B is used for this purpose.

The values null/false are formatted to false, everything else to true. %B is to format boolean in capital letters i.e. TRUE/FALSE

package com.logicbig.example.string;

public class StringPrintfBoolean {

public static void main(String[] args) {
System.out.printf("%b%n", null);
System.out.printf("%B%n", false);
System.out.printf("%b%n", "pilot");
System.out.printf("%b%n", 100);
System.out.printf("%B%n", "false");
System.out.printf("%B%n", 4 < 5);
}
}

Output

false
FALSE
true
true
TRUE
TRUE




See Also