Java 中 Boolean 的字串表示形式


要獲取 Boolean 的字串表示形式,請使用 toString() 方法。

首先,我們對 Boolean 物件使用 valueOf() 方法並設定一個字串值。

Boolean val = Boolean.valueOf("false");

然後使用 “toString() 方法對它進行表示。

val.toString();

讓我們看一個完整的示例,它以 Java 中的 Boolean(True 和 False)列印字串表示形式。

示例

 即時演示

public class Demo {
   public static void main(String[] args) {
      // false
      Boolean val = Boolean.valueOf("false");
      System.out.println("Displaying the string representation of Boolean");
      System.out.println(val.toString());
      // true
      val = Boolean.valueOf("true");
      System.out.println(val.toString());
   }
}

輸出

Displaying the string representation of Boolean
false
true

更新於: 2020 年 6 月 26 日

472 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.