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

Tutoriel de base Java

Contrôle de flux Java

Java tableau

Java orienté objet (I)

Java orienté objet (II)

Java orienté objet (III)

Gestion des exceptions Java

Java Liste (List)

Java Queue (file d'attente)

Java Map collection

Java Set collection

Entrée/sortie Java (I/O)

Reader Java/Writer

Autres sujets Java

Programme Java qui convertit la variable booléenne (boolean) en chaîne de caractères (string)

Java Examples Comprehensive

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 :

Example1:Utilisation de valueOf() pour convertir la valeur booléenne en chaîne de caractères

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.

Example2: Use toString() to convert boolean value to string

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.

Java Examples Comprehensive