Wednesday, December 25, 2024
Google search engine
HomeLanguagesJavaJava Program to Convert Boolean to String

Java Program to Convert Boolean to String

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);
    }
}


Output

Status: true

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);
    }
}


Output

Is Admin: false

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);
    }
}


Output

IsValid: true

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);
    }
}


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




// 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);
    }
}


Output

Is Online: true

RELATED ARTICLES

Most Popular

Recent Comments