Print Boolean Value Using the printf() Method in Java
-
Print Boolean By Using the
printf()
Method in Java -
Print Boolean By Using the
println()
Method in Java -
Print Boolean By Using the
print()
Method in Java

This tutorial introduces the printf()
method to print a boolean value in Java.
Boolean is a data type in Java that holds either true
or false
literals. It is mostly used with conditional statements. This article will teach us to print any boolean value using the printf()
method.
In Java, to print any value, we use the System.out.println()
method that works for boolean value as well, but if we want to print any formatted output to the console, then we use the printf()
method. This method is similar to the printf()
function of the C language.
In Java, this method belongs to the PrintStream
class and can print formatted output to the console. The syntax of this method is below.
public PrintStream printf(String format, Object... args)
This method takes two arguments. The first is a formatted string, and the second is an object to print.
The format string can be any of the following:
Format String | Object argument/value |
---|---|
b or B |
It represents a boolean value. |
h or H |
It represents a hexadecimal value. |
s or S |
It represents a string value. |
c or C |
It represents a character value. |
d |
It represents an integer value. |
f |
It represents a floating value. |
o |
It represents an octal integer value. |
x or X |
It represents a hexadecimal integer. |
e or E |
It represents a decimal number in computerized scientific notation. |
t or T |
It represents date and time conversion characters. |
Let’s understand the printing of boolean values with some examples.
Print Boolean By Using the printf()
Method in Java
In this example, we used the printf()
method of the PrintStream class to print boolean or formatted output to the console. This method is similar to the println()
method, except it takes two arguments.
See the example below.
public class SimpleTesting{
public static void main(String args[]) {
boolean isGreen = true;
findColor(isGreen);
isGreen = false;
findColor(isGreen);
}
static void findColor(boolean isGreen) {
if(isGreen) {
System.out.printf("Apple is green: %b%n",isGreen);
}else {
System.out.printf("Apple is green: %b%n",isGreen);
}
}
}
Output:
Apple is green: true
Apple is green: false
Print Boolean By Using the println()
Method in Java
If you don’t want formatted output or the printf()
method, you can use the most used method of Java, the println()
. This method does not require a format specifier, and you can get the result easily to the console.
See the example below.
public class SimpleTesting{
public static void main(String args[]) {
boolean isGreen = true;
findColor(isGreen);
isGreen = false;
findColor(isGreen);
}
static void findColor(boolean isGreen) {
if(isGreen) {
System.out.println("Apple is green: "+isGreen);
}else {
System.out.println("Apple is green: "+isGreen);
}
}
}
Output:
Apple is green: true
Apple is green: false
Print Boolean By Using the print()
Method in Java
You can even use the print()
method without any format specifier string and get the desired result to the console. This method is similar to the println()
method except for printing the result in the same line.
See the example below.
public class SimpleTesting{
public static void main(String args[]) {
boolean isGreen = true;
findColor(isGreen);
isGreen = false;
findColor(isGreen);
}
static void findColor(boolean isGreen) {
if(isGreen) {
System.out.print("Apple is green: "+isGreen);
}else {
System.out.print("\nApple is green: "+isGreen);
}
}
}
Output:
Apple is green: true
Apple is green: false
Related Article - Java Printf
Related Article - Java Boolean
- Toggle a Boolean Variable in Java
- Initialise Boolean Variable in Java
- Convert Boolean to Int in Java
- Boolean to String in Java
- String to Boolean in Java