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