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
import java.io.*;
class GFG {
public static void main(String[] args)
{
boolean status = true ;
String statusStr = Boolean.toString(status);
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
import java.io.*;
class GFG {
public static void main(String[] args)
{
boolean isAdmin = false ;
String isAdminStr = String.valueOf(isAdmin);
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
import java.io.*;
class GFG {
public static void main(String[] args)
{
boolean isValid = true ;
String isValidStr = "" + isValid;
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
import java.io.*;
class GFG {
public static void main(String[] args)
{
boolean isAvailable = false ;
String message = String.format( "Is Available: %b" ,
isAvailable);
System.out.println(message);
}
}
|
Output
Is 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
import java.io.*;
class GFG {
public static void main(String[] args)
{
boolean isOnline = true ;
String result = new StringBuilder()
.append( "Is Online: " )
.append(isOnline)
.toString();
System.out.println(result);
}
}
|
Share your thoughts in the comments
Please Login to comment...