java.lang.Boolean.toString(boolean b)方法範例


java.lang.Boolean.toString(boolean b) 返回表示指定布林值的String物件。如果指定的布林值為true,則字串“true”將被返回,否則字串“false”將被返回。

宣告

以下是java.lang.Boolean.toString()方法的宣告

public static String toString(boolean b)

引數

  • b - 布林轉換

返回值

此方法將返回指定的布林值的字串表示形式。

異常

  • NA

例子

下面的例子顯示了lang.Boolean.toString()方法的使用。

package com.yiibai;

import java.lang.*;

public class BooleanDemo {

   public static void main(String[] args) {

      // create 2 boolean primitives bool1, bool2
      boolean bool1, bool2;

      // assign values to bool1, bool2
      bool1 = true;
      bool2 = false;

      // create 2 String's s1, s2
      String s1, s2;

      /**
       *  static method is called using class name 
       *  assign string value of primitives bool1, bool2 to s1, s2
       */
      s1 = Boolean.toString(bool1);
      s2 = Boolean.toString(bool2);

      String str1 = "String value of boolean primitive " +bool1+ " is "  +s1;
      String str2 = "String value of boolean primitive " +bool2+ " is "  +s2;

      // print s1, s2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

讓我們來編譯和執行上面的程式,這將產生以下結果:

String value of boolean primitive true is true
String value of boolean primitive false is false