【发布时间】:2014-08-24 18:43:06
【问题描述】:
使用-Dkey=value 设置的任何系统属性都可以使用System.getProperty("key") 作为字符串检索。但是您也可以将某些系统属性转换为特定类型,例如:
Boolean.getBoolean("key"); //Returns true if value==true
Integer.getInteger("key"); //Returns value if exists & is number, else null
Long.getLong("key"); //Ditto as for Integer
是否还有其他具有static getSomeClass(String systemPropertyKey) 方法的类?为什么Byte、Float 或 Double 没有它们,而其他原始包装器却有(或者可能是什么原因)?
【问题讨论】:
-
我一直想知道为什么 Boolean、Integer 和 Long 有这些方法,因为它们与关联类的功能并不真正相关。我怀疑它们是历史上的意外,如果从头开始就不会提供。它们确实属于 System 或某些单独的 Properties 类。
-
@HotLicks 正是我的想法。这些方法附加到了错误的类,并且被误导了。