Java program to convert boolean variables into string

h‮t‬tps://www.theitroad.com

In Java, you can convert a boolean variable to a string using the Boolean.toString() method or by concatenating an empty string "" with the boolean variable. Here's an example program that demonstrates both approaches:

public class BooleanToStringExample {
    public static void main(String[] args) {
        boolean b1 = true;
        boolean b2 = false;

        // Using Boolean.toString() method
        String s1 = Boolean.toString(b1);
        String s2 = Boolean.toString(b2);

        System.out.println("Using Boolean.toString() method:");
        System.out.println("b1 as string: " + s1);
        System.out.println("b2 as string: " + s2);

        // Using string concatenation with empty string
        String s3 = "" + b1;
        String s4 = "" + b2;

        System.out.println("Using string concatenation with empty string:");
        System.out.println("b1 as string: " + s3);
        System.out.println("b2 as string: " + s4);
    }
}

This program creates two boolean variables b1 and b2, and converts them to strings using both approaches. The output of the program should look like this:

Using Boolean.toString() method:
b1 as string: true
b2 as string: false
Using string concatenation with empty string:
b1 as string: true
b2 as string: false