English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java Other Topics

Java Program Converts Boolean (boolean) Variable to String (string)

Comprehensive Java Examples

In this program, we will learn how to convert a boolean type variable to a string in Java.

To understand this example, you should be familiar with the followingJava ProgrammingTopic:

We can also use the toString() method of the Boolean class to convert a boolean variable to a string. For example,1Using valueOf() to convert a boolean value to a string

Example
  class Main {
    //public static void main(String[] args) {
    = true;1 Creating a boolean variable
    = true;2 boolean booleanValue
    //= false;
    //Using valueOf()
    String stringValue1 = String.valueOf(booleanValue1);
    String stringValue2 = String.valueOf(booleanValue2);
    System.out.println(stringValue1);    // true
    System.out.println(stringValue2);    // true
  }
}

In the above example, we use the valueOf() method of the String class to convert a boolean variable to a string.

We can also use the toString() method of the Boolean class to convert a boolean variable to a string. For example,2:使用toString()将布尔值转换为字符串

: Using toString() to convert a boolean value to a string

Example
  class Main {
    //public static void main(String[] args) {
    = true;1 Creating a boolean variable
    = true;2 boolean booleanValue
    // = false;
    // Using toString() to convert a boolean value to a string
    String stringValue1 = Boolean.toString(booleanValue1);
    String stringValue2 = Boolean.toString(booleanValue2);
    System.out.println(stringValue1);    // true
    System.out.println(stringValue2);    // true
  }
}

In the above example, the toString() method of the Boolean class converts a boolean variable to a string. Here, Boolean is a wrapper class. For more information, please visitJava Wrapper Class.

Comprehensive Java Examples