English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce programme, nous allons apprendre comment convertir une variable de type booléen en chaîne de caractères en Java.
Pour comprendre cet exemple, vous devriez comprendre ce qui suitProgrammation JavaSujet :
class Main { public static void main(String[] args) { //Create boolean variable boolean booleanValue1 = true; boolean booleanValue2 = false; //Convert boolean value to string //Utilisation de valueOf() String stringValue1 = String.valueOf(booleanValue1); String stringValue2 = String.valueOf(booleanValue2); System.out.println(stringValue1); // true System.out.println(stringValue2); // true } }
Dans l'exemple ci-dessus, nous utilisons la méthode valueOf() de la classe String pour convertir une variable booléenne en chaîne de caractères.
We can also use the toString() method of the Boolean class to convert a boolean variable to a string. For example,
class Main { public static void main(String[] args) { //Create boolean variable boolean booleanValue1 = true; boolean booleanValue2 = false; // Convert boolean value to string // Using toString() 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.