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 Long to String
The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a
4 min read
Java Program to Convert Date to String
The date type object can be converted to a string in Java using a large variety of in-built classes available in Java. Given a date, we need to convert the date to a given format of the string. Examples: Input: date = â2020-11-13âOutput: 2020-11-13 Input: date = â2020-12-14âOutput: 2020-12-14 Method
3 min read
Java Program to Convert String to Long
To convert a String to Long in Java, we can use built-in methods provided by the Long class. In this article, we will learn how to convert String to Long in Java with different methods. Example:In the below example, we use the most common method i.e. Long.parseLong() method to convert a string to a
3 min read
Java Program to Convert Enum to String
Given an enum containing a group of constants, the task is to convert the enum to a String. Methods: We can solve this problem using two methods: Using name() MethodUsing toString() Method Let us discuss both of them in detail and implementing them to get a better understanding of the same. Method 1
2 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 Convert OutputStream to String
OutputStream is an abstract class that is available in the java.io package. As it is an abstract class in order to use its functionality we can use its subclasses. Some subclasses are FileOutputStream, ByteArrayOutputStream, ObjectOutputStream etc. And a String is nothing but a sequence of character
2 min read
Java Program to Convert Char to Int
Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read