HOWTO · Java
How to Print Boolean Value Using the printf() Method in Java
This article introduces the `printf()` method to print a boolean value in Java.
On this page
A Java boolean stores either true or false. To include one in formatted console output, call printf() with a format string and the boolean value. Use %b for lowercase output or %B for uppercase output. If you do not need formatting, println() and print() are simpler options.
Print a Boolean with printf() in Java
System.out.printf() writes formatted text to the console. Include a conversion specifier for each value you want to format; extra arguments can remain unused. This self-contained example prints both boolean values with a descriptive label:
Run this example:
public class Main {
public static void main(String[] args) {
boolean ready = true;
boolean complete = false;
System.out.printf("Ready: %b%n", ready);
System.out.printf("Complete: %b%n", complete);
}
}
%n adds a platform-appropriate line separator, so each result appears on its own line. The next section explains how to change the letter case of the displayed values.
Use %b and %B for Boolean Output
Use %b to format a boolean as lowercase true or false. Use %B for uppercase TRUE or FALSE. The specifier must correspond to the value in the same position among the arguments. This example shows the two forms side by side:
public class Main {
public static void main(String[] args) {
boolean enabled = true;
boolean archived = false;
System.out.printf("%b %B%n", enabled, archived);
}
}
The first conversion uses the first supplied value, and the second conversion uses the next one. The format string and argument list should stay aligned when you change or add values. If you only want to display a boolean without controlling its case or arranging other text, use a direct print method instead.
Print a Boolean with println() or print()
System.out.println(value) writes the value and then a line separator. It accepts a boolean directly, so no conversion specifier or string concatenation is needed. System.out.print(value) writes the value without ending the line; add println() or %n later if subsequent output should start on a new line.
public class Main {
public static void main(String[] args) {
boolean connected = true;
System.out.println("Connected: " + connected);
boolean saved = false;
System.out.print("Saved: ");
System.out.println(saved);
}
}
Choose println() when each value should finish a line. Choose print() when you want to continue writing on the same line, such as when assembling a prompt from several pieces.
Common Format and Output Pitfalls
A format specifier consumes a corresponding argument. Keep the count and order of conversions aligned with the values in the call; otherwise, the text may not represent the values you intended. For example, %b followed by two boolean arguments formats only the first argument, while a second conversion is needed to display the other one.
printf() does not automatically finish a line. Add %n to the format string when output should end with a line break. This is preferable to embedding a platform-specific newline character in a format string. By contrast, println() already ends its output with a line separator, while print() does not.
Use %b and %B only when their lowercase or uppercase presentation is useful. For an ordinary boolean display, println() is shorter and makes the line behavior clear. Keep the boolean value as a boolean in your program; the format specifier handles its textual representation when writing formatted output.