相信很多人被Boolean.getBoolean(String name)欺骗过,想当然的认为它是将“true”或者“false”转换为Boolean类型的API。错,完全不是!下面我们就详细讲一下Boolean.getBoolean(String name)的用法。

1 用法

首先我们看下Boolean.getBoolean(String name)的源码:

public static boolean getBoolean(String name) {
boolean result = false;
try {
result = parseBoolean(System.getProperty(name));
} catch (IllegalArgumentException | NullPointerException e) {
}
return result;
}

从源代码中可以看出Boolean.getBoolean首先会根据name从系统属性中获取name属性的值,然后对name属性对应的值进行转换。

正确的用法用法应该是:

public static void main(String[] args) {
String value = "true";
String key= "key";
System.setProperty(key, value);
Boolean flag = Boolean.getBoolean(key);
if(flag) {
System.out.println("key is true");
} else {
System.out.println("key is not true");
}
}

如果想将“true”或者“false”转换为Boolean类型值,可以使用Boolean.parseBoolean(String s)。

Boolean.getBoolean用法的更多相关文章

  1. Boolean.parseBoolean("true") 和 Boolean.getBoolean("true");的区别及用法

    正确用法:boolean repeatIndicator = Boolean.valueOf("true").booleanValue();或者也可以使用Boolean.parse ...

  2. c#的Boolean.Parse用法

    bool val; string input; input = bool.TrueString; val = bool.Parse(input); Console.WriteLine("'{ ...

  3. 022、Java中boolean的用法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. PropertiesConfiguration的用法

    PropertiesConfiguration是一个配置文件的加载工具类,封装了从配置文件里获取值并转化为基本数据类型的方法. 使用org.apache.commons.configuration2中 ...

  5. sharepreferce支持boolean,string类型

    public class SharePrefersUtils { private static final String name="cogi"; public static bo ...

  6. 1、关于Boolean(2015年05月30日)

    背景:刚在看Effective Java,看到一段关于Boolean提供一个返回实例的静态方法的例子,便去看了下Boolean的源码,发现有些内容是之前没注意到的,于是便有了下面这些. 1. Bool ...

  7. Boolean类源码分析

    Boolean类里面的常量: Boolean.TRUE:这个是调用Boolean的构造函数,新建了一个Boolean对象,所以TRUE是Boolean类型的.用来避免每次都创建新的Boolean对象, ...

  8. java.lang.Boolean 类源码解析

    Boolean源码比较简单. public final class Boolean implements java.io.Serializable, Comparable<Boolean> ...

  9. java封装数据类型——Boolean

    众所周知,java对常见的原始数据类型都提供了对应的封装类型,增加一些常用的特性(如 计算hash值.比较相等.类型转换等),以扩展他们对数据处理的能力,使得他们更好地适应面向对象编程的各种场景.今天 ...

随机推荐

  1. python拓展2 collections模块与string模块

    知识内容 1.collections模块介绍 2.collections模块使用 3.string模块介绍及使用 一.collections模块介绍 collections模块中提供了很多python ...

  2. flask框架预备知识

    1.web预备知识 2.flask介绍 3.web框架的本质及分类 4.flask安装与基本设置 1.web预备知识 HTTP协议:https://www.cnblogs.com/wyb666/p/9 ...

  3. python带参数装饰器使用

    # -*- coding: utf-8 -* """TensorFlow指定使用GPU工具类 author: Jill usage: 方法上加@tf_with_devic ...

  4. 谈谈 Python 程序的运行原理

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,谈谈 Python 程序的运行原理 这篇文章准确说是『Python 源码剖析』的 ...

  5. HTML5 Canvas ( 矩形的绘制 ) rect, strokeRect, fillRect

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. delphi XE7 判断手机返回键

    Using the Android Device's Back Button To make your application handle when users press the Back but ...

  7. http chunked

    http chunked传输:将信息分段传输 好处: 不用指定content-length字段(总的要传输文件信息的长度),即可以将一整段信息分为若干段分别发送,最后发送chunked长度为0的信息表 ...

  8. python 学习备忘

    list列表排序 def listdir(path): #返回一个排序后的目录list files=os.listdir(path) files.sort(key=lambda x:str(x[:-4 ...

  9. 有3D效果的进度条

    // The Unofficial Newsletter of Delphi Users - Issue #12 - February 23rd, 1996 unit Percnt3d; (* TPe ...

  10. chrome innerHTML赋值

    例如: document.getElementById('id').innerHTML=data; 如果data的值是: "<div><tr><td>1& ...