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

将Java字符串对象转换为布尔对象

È possibile creare un oggetto String in Java utilizzando un testo di stringa.

String myStr = "Amit";

Un altro modo per creare un oggetto String è utilizzare la parola chiave new.

String myStr = new String("Hello!");

Usiamo il primo metodo per creare un oggetto String.

String str = "true";

Ora, usando ilvalueOf()Il metodo converte un oggetto String in un oggetto Boolean. Lo abbiamo già utilizzato su un oggetto Boolean.

Boolean bool = Boolean.valueOf(str);

Ora vediamo un esempio completo per mostrare come convertire un oggetto String in un oggetto Boolean.

Esempio

public class Demo {
   public static void main(String[] args) {
      String str = "true";
      //Conversione
      Boolean bool = Boolean.valueOf(str);
      System.out.println(bool);
   }
}

Risultato di output

Verità