Java Program to Convert Boolean to String
Last Updated :
22 Sep, 2023
In Java programming, you might come across the need to convert a simple "true" or "false" boolean value into a String. It may seem like a challenging task, but fear not! In this article, You will explore some methods to convert a boolean value to a string in Java
Method for Boolean to String Conversion in Java
The method for Conversion from Boolean to String is mentioned below:
- Using Boolean.toString()
- Using String.valueOf()
- Concatenation with an Empty String
- Using String.format()
- StringBuilder or StringBuffer
1. Using Boolean.toString()
Boolean.toString()
is a static method in Java's Boolean
class used to convert a boolean value to its string representation. It returns "true" if the input boolean is true
, and "false" if the input boolean is false
. This method is helpful when you need to display or manipulate boolean values as strings.
Syntax
String Str = Boolean.toString(booleanValue);
Below is the implementation of the above method:
Java
// Java Program to demonstrate
// Boolean.toString()
import java.io.*;
// Driver Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a boolean variable
boolean status = true;
// Converting the boolean value 'status' to a string
String statusStr = Boolean.toString(status);
// Printing the message along with the statusStr
System.out.println("Status: " + statusStr);
}
}
2. Using String.valueOf()
String.valueOf()
is a static method in Java's String
class that converts various data types, including booleans, characters, and numbers, into their corresponding string representations.This method provides a convenient way to convert different data types to strings without using constructors or concatenation.
Syntax
String Str = String.valueOf(booleanValue);
Below is the implementation of the above method:
Java
// Java Program to demonstrate
// String.valueOf()
import java.io.*;
// Driver Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a boolean variable
boolean isAdmin = false;
// Converting the boolean value 'isAdmin' to a
// string
String isAdminStr = String.valueOf(isAdmin);
// Printing the message with isAdminStr variable
System.out.println("Is Admin: " + isAdminStr);
}
}
3. Concatenation with an Empty String
Concatenation with an empty string in Java is a technique used to convert non-string data types to strings. By appending an empty string (""
) to a value of any data type, Java implicitly converts that value to its string representation.
Syntax
String Str = "" + booleanValue ;
Below is the implementation of the above method:
Java
// Java Program to demonstrate
// Concatenation with an Empty String
import java.io.*;
// Driver Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a boolean variable
boolean isValid = true;
// Converting the boolean 'isValid' to a string
String isValidStr = "" + isValid;
// Printing the message which shows the validity
// status
System.out.println("IsValid: " + isValidStr);
}
}
4. Using String.format()
String.format()
is a Java method for creating formatted strings. It replaces placeholders like %s
or %d
with corresponding values. It's useful for dynamic and visually appealing output, making it easier to display variables within a fixed text template.
Syntax
String Str = String.format("%b",booleanValue);
Below is the implementation of the above method:
Java
// Java Program to demonstrate
// String.format()
import java.io.*;
// Driver Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a boolean variable
boolean isAvailable = false;
// Crafting a message that show availability status
String message = String.format("Is Available: %b",
isAvailable);
// Printing the formatted message
System.out.println(message);
}
}
OutputIs Available: false
5. StringBuilder or StringBuffer
StringBuilder
and StringBuffer
are Java classes for working with strings. StringBuilder
is faster in single-threaded scenarios, while StringBuffer
is safer for multi-threading. Both classes have similar methods for string manipulation.
Syntax
String Str = new StringBuilder().append(booleanValue).toString();
Below is the implementation of the above method:
Java
// Java Program to demonstrate
// StringBuilder or StringBuffer
import java.io.*;
// Driver Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a boolean variable
boolean isOnline = true;
// Creating a StringBuilder to build the result
// string
String result = new StringBuilder()
.append("Is Online: ")
.append(isOnline)
.toString();
// Printing the result string
System.out.println(result);
}
}
Similar Reads
Java Program to Convert String to boolean In Java, to convert a string to a Boolean, we can use Boolean.parseBoolean(string) to convert a string to a primitive Boolean, or Boolean.valueOf(string) to convert it to a Boolean object. The Boolean data type only holds two possible values which are true and false. If the string equals "true" (ign
3 min read
Java Program to convert integer to boolean Given a integer value, the task is to convert this integer value into a boolean value in Java. Examples: Input: int = 1 Output: true Input: int = 0 Output: false Approach: Get the boolean value to be converted. Check if boolean value is true or false If the integer value is greater than equal to 1,
2 min read
Java Program to convert boolean to integer Given a boolean value, the task is to convert this boolean value into an integer value in Java. Examples: Input: boolean = true Output: 1 Input: boolean = false Output: 0 Approach: Get the boolean value to be converted.Check if boolean value is true or falseIf the boolean value is true, set the inte
2 min read
Java Program to Convert String to Object In-built Object class is the parent class of all the classes i.e each class is internally a child class of the Object class. So we can directly assign a string to an object. Basically, there are two methods to convert String to Object. Below is the conversion of string to object using both of the me
2 min read
Java Program to Compare two Boolean Arrays Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways: By using Java built-in method that is .equals() method.By using the Naive approach. Examples: Input : A = [true , true , false] A1 = [true, true, false] Output: Both the ar
3 min read
Program to check if the String is Null in Java In Java, checking if a string is null is essential for handling null-safe conditions and preventing runtime errors. To check if a string is null in Java, we can use the "==" operator that directly compares the string reference with null.Example:The below example demonstrates how to check if a given
1 min read