Google News
logo
Java Program to convert boolean variables into string
Yes, we can convert boolean variables into string in Java. Here is a Java program to demonstrate this:
Program :
public class Main {
    public static void main(String[] args) {
        
        // create a boolean variable
        boolean isTrue = true;

        // convert boolean to String using valueOf method
        String str = String.valueOf(isTrue);

        System.out.println("Boolean value: " + isTrue);
        System.out.println("String value: " + str);
    }
}
Output :
Boolean value: true
String value: true
In this program, we first declare a boolean variable isTrue and initialize it with the value true.

We then convert this boolean variable into a string variable str using the String.valueOf() method. This method takes a boolean value as an argument and returns the string "true" or "false" depending on the value of the boolean.

Finally, we print both the boolean and string values using the System.out.println() method.