How To Convert Boolean To String in Java

The Tutorial we are going to learn is How to Convert Boolean to String in Java. Let us know about both data types before we start: The Boolean data type is a basic data type in Java, which means it comes pre-installed with the language. Only true or false can be the value of a Boolean data type. These are reserved terms in the programming language and hence they cannot be used to identify variables, functions, classes, or objects! A switch, in this example true or false, is a fundamental idea in every programming language. As a result, the outcome of a Boolean operation is either true or false (lowercase in Java code). Other Java programming capabilities rely on a Boolean value intrinsically, which may not be clear at first.

In computer programming, a string is a sequence of characters that is used either as a constant or as a variable. The latter may allow for the mutation of its elements and the modification of their length, or it may be fixed (after creation). In computing, a string is a data type that is frequently implemented as an array of bytes (or words) that stores a succession of items, most commonly characters, using some character encoding.

 

What’s The Approach?

We can solve this problem in two ways one by using the String.valueOf() and Boolean.toString()

  • Create a class

 

  • Declare the Main Function

 

 

  • Create a String and use the String.valueOf(Boolean_variable).

Or

  • Create a String and use the Boolean.toString(Boolean_variable)

 

Also Read:How to Convert a String to Date in Java

 

Java Program To convert Boolean to String

 

Method 1:-

/*

 * TechDecode Tutorials

 *

 * How to Covert Boolean to String

 *

 */

public class Boolean_to_String

{

    public static void main(String args[])

    {

        //declaring a Boolean Varibale

        boolean b=true; 

        //Converting

        String s=String.valueOf(b);

        //Printing the String

        System.out.println(s); 

    }

}

 

Method 2:-

 

/*

 * TechDecode Tutorials

 *

 * How to Covert Boolean to String

 *

 */

public class Boolean_to_String

{

    public static void main(String args[])

    {

        //declaring a Boolean Varibale

        boolean b=true; 

        //Converting

        String s=Boolean.toString(b);

        //Printing the String

        System.out.println(s); 

    }

}

 

Output: – 

Output of the Program

Leave a Reply

Your email address will not be published.