Java Convert Boolean to String Example

In this article, we will show you how to convert Java wrapper class Boolean or primitive type boolean to String code examples. There are different ways we can convert Java Boolean to String.
In this example, we will demonstrate converting Boolean to String in 2 ways.
  1. Convert using String.valueOf(b)
  2. Convert using Boolean.toString(b)
/**
 * Class to Convert Double To String Example
 * @author javaguides.net
 *
 */

public class ConvertBooleanToStringExamples {

 public static void main(String[] args) {

  /*
   * Method 1: using valueOf() method of String class.
   */
  boolean flag = true;
  String strBoolean = String.valueOf(flag);
  System.out.println("Convert boolean to string using valueOf method ::" + strBoolean);

  /*
   * Method 2: using toString() method of Boolean class
   */
  String strBoolean1 = Boolean.toString(flag);
  System.out.println("Convert boolean to string using Boolean.toString method ::" + strBoolean1);
 }

}
Output:
Convert boolean to string using valueOf method ::true
Convert boolean to string using Boolean.toString method ::true

Related Java String Conversion Examples

Comments