当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.lang.Boolean.getBoolean()用法及代码示例



描述

这个java.lang.Boolean.getBoolean(String name)当且仅当由参数命名的系统属性存在且等于字符串 "true" 时,才返回 true。系统属性可通过 getProperty 访问,getProperty 是 System 类定义的方法。

如果没有指定名称的属性,或者指定名称为空或为空,则返回 false。

声明

以下是声明java.lang.Boolean.getBoolean()方法

public static boolean getBoolean(String name)

参数

nameâˆ' 系统属性名称

返回值

此方法返回系统属性的布尔值。

异常

NA

示例

下面的例子展示了 lang.Boolean.getBoolean() 方法的用法。

package com.tutorialspoint;

import java.lang.*;

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

相关用法


注:本文由纯净天空筛选整理自 Java.lang.Boolean.getBoolean() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。