Java Program to convert boolean variables into string

Example 1: Convert boolean to string using valueOf()

class BooleanToString {
  public static void main(String[] args) {

    // create two boolean variables
    boolean booleanValue1 = true;
    boolean booleanValue2 = false;

    // convert boolean to string using valueOf()
    String stringValue1 = String.valueOf(booleanValue1);
    String stringValue2 = String.valueOf(booleanValue2);

    System.out.println(stringValue1);  
    System.out.println(stringValue2);  
  }
}
Output
true
true

Example 2: Convert boolean to String using toString()

class BooleanToString {
  public static void main(String[] args) {

    // create two boolean variables
    boolean booleanValue1 = true;
    boolean booleanValue2 = false;

    // convert boolean to string using toString()
    String stringValue1 = Boolean.toString(booleanValue1);
    String stringValue2 = Boolean.toString(booleanValue2);

    System.out.println(stringValue1);  
    System.out.println(stringValue2);
  }
}
Output
true
true

Leave a Reply