`

Boolean.getBoolean(String name)方法分析

    博客分类:
  • java
阅读更多

 

 Boolean.getBoolean(String name)根据方法名可能认为当name为"true"时,返回true,当为其它值或空时为false

    查看Boolean.getBoolean(String name)源码解析如下:

 

        returns  true  if and only if the system property  named by the argument exists and is equal to the string 

    true. 

        根据源码解析可知,该方法是查询系统参数name,如果name值为true,则返回true,当不存在或是其它值时返回false。

    例如:

 

         System.setProperty("name", "true");

         Boolean.getBoolean("name");  //返回true

         Boolean.getBoolean("true");   //返回false

 

 

         查看源码实现如下:

        public static boolean getBoolean(String name) {

        boolean result = false;

        try {

            result = toBoolean(System.getProperty(name));

        } catch (IllegalArgumentException e) {

        } catch (NullPointerException e) {

        }

        return result;

       }

       可知,该方法是查看系统参数。

       如果想坚持字符串是否为true,可用下面的方法:

        Boolean.parseBoolean("true") //jdk1.5之后推荐使用。

        Boolean("true").booleanValue()

 

---------------------------------------------

1、今天遇到这样一件事:想把String类型的true和false转换成boolean的原生类型,于是顺手的用Boolean.<alt+/>(快捷键),jdk提供了一个static 的 getBoolean(name)方法,调用之后并不是我预期的结果。最先值得怀疑的应该是自己代码有问题。细查后原来是被此方法名误解了。

 

2、这个方法名起的很邪恶。就是被他的字面意思给误解了。

 

3、仔细看了看文档

jdk 写道
Returns true if and only if the system property named by the argument exists and is equal to the string "true". (Beginning with version 1.0.2 of the JavaTM platform, the test of this string is case insensitive.) A system property is accessible through getProperty, a method defined by the System class. 

 当且仅当系统属性的名字存在且他的值为“true”是才返回true,不知道大家在第一次有没有被误导的,我反正被他欺骗了。

 

4提供这样的一个方法究竟有什么用处了。我相信作者van Hoff最初的想法是很好的在没有这个方法之前,如果想获取系统属性的值转换成Boolean类型操作的话通常就是:

Java代码  收藏代码
  1. String value = System.getProperty(key);  
  2. boolean b = Boolean.valueOf(value);  
  3. //TODO  

 而现在可以直接用getBoolean方法了,其实他也就是在这方法里封装了一下string——>boolean转换的步骤,请看源代码:

Java代码  收藏代码
  1. public static boolean getBoolean(String name) {  
  2.        boolean result = false;  
  3.        try {  
  4.            result = toBoolean(System.getProperty(name));  
  5.        } catch (IllegalArgumentException e) {  
  6.        } catch (NullPointerException e) {  
  7.        }  
  8.        return result;  
  9.    }  

 5、对此方法的一些见解:本应是作为System类中的一个方法,现在把他放在Boolean类中,试想如果是这样写:

Java代码  收藏代码
  1. System.getPropertyAsBoolean(name)  

是不是见名知义了呢?

 

 

6、瞎想:作者van Hoff本是好意,没想到弄巧成拙,误导不少初次使用此方法的人,查看了下写System类和Boolean类的作者分别为:unascribed和van Hoff,他们正喝着咖啡,一边聊着天,一边写着代码,聊着聊着······(由大家补充吧),结果。。。

分享到:
评论

相关推荐

    java jdbc帮助类 v1.0 自带连接池

    java jdbc帮助类 v1.0 自带连接池 boolean delete(java.lang.String sql) 删除单条记录的方法,适用于... boolean update(java.lang.String sql, java.lang.String poolName) 更新一条记录,适用与简单的update语句

    Boolean.Functions.and.Computation.Models,.Clote,.Kranakis,.Springer,.2002

    This textbook presents a survey of research on boolean functions, circuits, parallel computation models, function algebras, and proof systems. Its main aim is to elucidate the structure of "fast" ...

    jdbc 帮助类 java 自带连接池 v1.01

    void setPoolName(java.lang.String poolName) boolean update(java.lang.String sql, java.util.Map&lt;java.lang.Integer,java.lang.Object&gt; elements) 根据输入的参数执行更新操作 boolean update(java....

    boolean.py:在一个模块中实现布尔代数

    “boolean.py”是一个实现布尔代数的小型库。 它定义了两个基本元素 TRUE 和 FALSE,以及一个可以采用这两个值之一的 Symbol 类。 计算是根据 AND、OR 和 NOT 完成的 - 其他组合如 XOR 和 NAND 未实现,但可以使用 ...

    生成系统日志

    at attGateWay.AttOp.exeQuery(String fromStrIn, String whereStrIn, Int32 SearchType, Boolean IsGetCount, String strRelation, Boolean IsCache, String gh, Int32& ResultCount) 请问此问题如何解决?我在...

    javaSE_tedu_day.zip

    3.check(String name,String pwd)boolean 4.updateInfo(String name,String email):void 5.updatePwd(String newPwd):void main方法: 1.注册 2.登录 0.退出登录 登陆成功之后,进入用户管理 1.修改个人...

    Test_sget_boolean.rar_sget-boolean

    Test sget boolean extends DxTestCase Source Code for Linux.

    简介JavaScript中Boolean.toSource()方法的使用

    JavaScript Boolean.toSource()方法返回表示对象的源代码的字符串。 注意:此方法不会在Internet Explorer中运行。 语法 boolean.toSource() 下面是参数的详细信息: NA 返回值 返回表示对象的源代码的字符串。 ...

    tab_boolean.zip

    labview 控件 tab_control 与 boolean按钮 配合使用 切换分页。 给朋友,兼服务大众。 很简单的小技巧,不过对于G语言不熟悉的可能要摸索花时间。 网上没有看到类似的,所以传一个范本上来。 不唯一,还有别的处理...

    第三部分:学会在mimics中操作Boolean.avi

    Mimics Medical 20.0学习视频教程

    boolean.7z

    多边形布尔运算

    Boolean RT.zip

    BooleanRT, 实现2个物体间的布尔运算 The extension provides the following functions: 1- Real-time or per-click Boolean execution. 2- Material, UV and texture preservation. 3- Saving to prefab. 4- ...

    Chartlet.dll

    ID System.String ShowCopyright System.Boolean InflateWidth System.Int32 InflateHeight System.Int32 ShowErrorInfo System.Boolean Colorful System.Boolean AutoBarWidth System.Boolean Alpha3D System.Byte ...

    Json解析ParsreTools.zip

    object = jsonObject2.getBoolean(varName[i]); } else if (type.equals("class java.lang.String")) { object = jsonObject2.getString(varName[i]); } else if (type.contains("java.util.List...

    StringAPI.java

    boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写 String concat(String str):将指定字符串连接到此字符串的结尾。 等价于用“+” String substring(int beginIndex):返回一个新...

    Introduction to Boolean Algebras.pdf

    Introduction to Boolean Algebras.pdf Introduction to Boolean Algebras.pdf

    T_sput_boolean_1.rar_Dot.

    package dot.junit.opcodes.sput_boolean.d Source Code for Andriod.

    发送程序-源码

    function fUpdatePicturePath(Const aAcc_ID_Only, aPicturePath: String): boolean; //保存DVR1图片路径 public { Public declarations } end; var frmMain: TfrmMain; zzSourceDir: string; ...

    基于Java通讯开发jms源代码 (jms通讯开发源码)

    setBooleanProperty(String name, boolean value) throws JMSException; void setByteProperty(String name, byte value) throws JMSException; void setShortProperty(String name, short value) throws ...

Global site tag (gtag.js) - Google Analytics