Java - Boolean getBoolean() 方法



描述

Java Boolean getBoolean(String name) 方法僅當由引數命名的系統屬性存在且等於字串“true”時才返回 true。系統屬性可以透過 getProperty 方法訪問,該方法由 System 類定義。

如果沒有指定名稱的屬性,或者指定的名稱為空或為 null,則返回 false。

宣告

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

public static boolean getBoolean(String name)

引數

name − 系統屬性名稱

返回值

此方法返回系統屬性的布林值。

異常

使用系統屬性 True 獲取布林值示例

以下示例演示了 Boolean getBoolean() 方法的使用,其中系統屬性存在且值為 true 和隨機字串“abcd”。在此程式中,我們建立了兩個布林變數。使用 System.setProperty(),我們建立了兩個系統屬性 demo1 和 demo2,其值分別為 true 和 abcd。現在使用 getBoolean() 方法,檢索系統屬性的值並列印結果。

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

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

      /**
       *  using System class's setProprty method, set the values of
       *  system properties demo1, demo2.
       */
      System.setProperty("demo1","true");
      System.setProperty("demo2","abcd");

      // retrieve value of system properties to s1, s2
      String s1 = System.getProperty("demo1");
      String s2 = System.getProperty("demo2");

      // assign result of getBoolean on demo1, demo2 to bool1, bool2
      bool1 = Boolean.getBoolean("demo1");
      bool2 = Boolean.getBoolean("demo2");

      String str1 = "boolean value of system property demo1 is " + bool1;
      String str2 = "System property value of demo1 is " + s1;
      String str3 = "boolean value of system property demo2 is " + bool2;
      String str4 = "System property value of demo2 is " + s2;

      // print bool1, bool2 and s1, s2 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
      System.out.println( str4 );
   }
}

輸出

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

boolean value of system property demo1 is true
System property value of demo1 is true
boolean value of system property demo2 is false
System property value of demo2 is abcd

使用系統屬性獲取布林值示例

以下示例演示了 Boolean getBoolean() 方法的使用,其中系統屬性存在且值為 false 和隨機字串“abcd”。在此程式中,我們建立了兩個布林變數。使用 System.setProperty(),我們建立了兩個系統屬性 demo1 和 demo2,其值分別為 false 和 abcd。現在使用 getBoolean() 方法,檢索系統屬性的值並列印結果。

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

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

      /**
       *  using System class's setProprty method, set the values of
       *  system properties demo1, demo2.
       */
      System.setProperty("demo1","false");
      System.setProperty("demo2","abcd");

      // retrieve value of system properties to s1, s2
      String s1 = System.getProperty("demo1");
      String s2 = System.getProperty("demo2");

      // assign result of getBoolean on demo1, demo2 to bool1, bool2
      bool1 = Boolean.getBoolean("demo1");
      bool2 = Boolean.getBoolean("demo2");

      String str1 = "boolean value of system property demo1 is " + bool1;
      String str2 = "System property value of demo1 is " + s1;
      String str3 = "boolean value of system property demo2 is " + bool2;
      String str4 = "System property value of demo2 is " + s2;

      // print bool1, bool2 and s1, s2 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
      System.out.println( str4 );
   }
}

輸出

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

boolean value of system property demo1 is false
System property value of demo1 is false
boolean value of system property demo2 is false
System property value of demo2 is abcd

使用不存在的系統屬性獲取布林值示例

以下示例演示了 Boolean getBoolean() 方法的使用,其中系統屬性不存在。在此程式中,我們建立了兩個布林變數。現在使用 getBoolean() 方法,檢索系統屬性的值並列印結果。由於系統屬性不存在,因此 getBoolean 方法返回 false。

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

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

      // retrieve value of system properties to s1, s2
      String s1 = System.getProperty("demo1");
      String s2 = System.getProperty("demo2");

      // assign result of getBoolean on demo1, demo2 to bool1, bool2
      bool1 = Boolean.getBoolean("demo1");
      bool2 = Boolean.getBoolean("demo2");

      String str1 = "boolean value of system property demo1 is " + bool1;
      String str2 = "System property value of demo1 is " + s1;
      String str3 = "boolean value of system property demo2 is " + bool2;
      String str4 = "System property value of demo2 is " + s2;

      // print bool1, bool2 and s1, s2 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
      System.out.println( str4 );
   }
}

輸出

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

boolean value of system property demo1 is false
System property value of demo1 is null
boolean value of system property demo2 is false
System property value of demo2 is null
java_lang_boolean.htm
廣告

© . All rights reserved.