之前試了 getBoolean 方法想要將字串轉成 boolean 型態,但是傳了 "true"、"True"、"TRUE" 最後的結果總是 false,用了google 查了一下,原來要使用 getBoolean 你必須設定 System 的 properties。 其實在
Java Doc 已經有提及到了,敘述如下:
Returns true if and only if the system property named by the argument exists and is equal to the string "true".
If there is no property with the specified name, or if the specified name is empty or null, then false is returned.
首先你可以透過 System.getProperties() 來取的目前執行你程式的一些系統參數設定,它主要是以 <key>=<value> 的形式顯示出來,所以當你要查詢某一個屬性值時,你可以透過 System.getProperty("key") 的方式得到 其對應的 value 值,如果該 key 不存在則回傳 null。
另外除了透過 getBoolean 可以轉換成 基本型態的 boolean 值外,我們還可以透過 Boolean.valueOf(String s) 的方式轉換成 boolean的 Wrapper class型態。但 Boolean.valueOf(String s) 方法回傳 true 只有當字串為 "true"(大小寫無關) 時,其餘都回傳 false。
我們可以透過 setProperty 來設定額外的系統參數,其設定型態也是 key/value的方式 ,setProperty ("key", "value"),除了設定 System.setProperty("TRUE", "TRUE"); 來讓 Boloean.getBoolean("TRUE") 來回傳 true ,我們也可以設定讓 Boloean.getBoolean("YES")也可以回傳 true 的值。
範例程式:
public class BooleanDemo {
public static void main(String[] args) {
System.out.println(System.getProperties());
System.out.println("======================================");
System.out.println(System.getProperty("java.runtime.name"));
System.out.println("System.getProperty(\"true\") = " + System.getProperty("true"));
System.out.println("System.getProperty(\"TRUE\") = " + System.getProperty("TRUE"));
System.out.println("System.getProperty(\"True\") = " + System.getProperty("True"));
System.out.println("Boolean.getBoolean(\"true\") = " + Boolean.getBoolean("true"));
System.out.println("Boolean.getBoolean(\"TRUE\") = " + Boolean.getBoolean("TRUE"));
System.out.println("Boolean.valueOf(\"true\") = " + Boolean.valueOf("true"));
System.out.println("Boolean.valueOf(\"TRUE\") = " + Boolean.valueOf("TRUE"));
System.setProperty("TRUE", "TRUE");
System.setProperty("YES", "True");
System.out.println("Boolean.getBoolean(\"TRUE\") = " + Boolean.getBoolean("TRUE"));
System.out.println("Boolean.getBoolean(\"YES\") = " + Boolean.getBoolean("YES"));
System.out.println("Boolean.getBoolean(\"yes\") = " + Boolean.getBoolean("yes"));
System.clearProperty("YES");
System.out.println("Boolean.getBoolean(\"YES\") = " + Boolean.getBoolean("YES"));
}
}
輸出結果:
{java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=C:\Program Files\Java\jre7\bin, java.vm.version=23.25-b01, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.country=TW, user.script=, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=Service Pack 3, java.vm.specification.name=Java Virtual Machine Specification, user.dir=C:\Documents and Settings\Josh\JavaCode\BooleanDemo, java.runtime.version=1.7.0_25-b17, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:\Program Files\Java\jre7\lib\endorsed, os.arch=x86, java.io.tmpdir=R:\TEMP\, line.separator=
, java.vm.specification.vendor=Oracle Corporation, user.variant=, os.name=Windows XP, sun.jnu.encoding=MS950, java.library.path=C:\Program Files\Java\jre7\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\Ruby200\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Perforce;C:\Program Files\Java\jdk1.7.0_07\bin;C:\Program Files\Android\android-sdk\tools;C:\Program Files\Android\android-sdk\platform-tools;C:\Program Files\apache-ant-1.9.1\bin;C:\Program Files\Perforce\Server;C:\Program Files\eclipse-SDK-4.2-win32;;., java.specification.name=Java Platform API Specification, java.class.version=51.0, sun.management.compiler=HotSpot Client Compiler, os.version=5.1, user.home=C:\Documents and Settings\Josh, user.timezone=, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=UTF-8, java.specification.version=1.7, java.class.path=C:\Documents and Settings\Josh\JavaCode\BooleanDemo\bin, user.name=Josh, java.vm.specification.version=1.7, sun.java.command=BooleanDemo, java.home=C:\Program Files\Java\jre7, sun.arch.data.model=32, user.language=zh, java.specification.vendor=Oracle Corporation, awt.toolkit=sun.awt.windows.WToolkit, java.vm.info=mixed mode, sharing, java.version=1.7.0_25, java.ext.dirs=C:\Program Files\Java\jre7\lib\ext;C:\WINDOWS\Sun\Java\lib\ext, sun.boot.class.path=C:\Program Files\Java\jre7\lib\resources.jar;C:\Program Files\Java\jre7\lib\rt.jar;C:\Program Files\Java\jre7\lib\sunrsasign.jar;C:\Program Files\Java\jre7\lib\jsse.jar;C:\Program Files\Java\jre7\lib\jce.jar;C:\Program Files\Java\jre7\lib\charsets.jar;C:\Program Files\Java\jre7\lib\jfr.jar;C:\Program Files\Java\jre7\classes, java.vendor=Oracle Corporation, file.separator=\, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.desktop=windows, sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86}
======================================
Java(TM) SE Runtime Environment
System.getProperty("true") = null
System.getProperty("TRUE") = null
System.getProperty("True") = null
Boolean.getBoolean("true") = false
Boolean.getBoolean("TRUE") = false
Boolean.valueOf("true") = true
Boolean.valueOf("TRUE") = true
Boolean.getBoolean("TRUE") = true
Boolean.getBoolean("YES") = true
Boolean.getBoolean("yes") = false
Boolean.getBoolean("YES") = false
另外我在
Boolean.parseBoolean(“1”) = false…? 此篇討論文章,看到有人提供自己自製的方法來設定那些字串要回傳 true。
程式碼如下:
private boolean convertToBoolean(String value) {
if ("1".equalsIgnoreCase(value) ||
"yes".equalsIgnoreCase(value) ||
"true".equalsIgnoreCase(value)||
"on".equalsIgnoreCase(value))
return true;
return false;
}
參考資料:
getBoolean(String str) and valueOf(String str) of Boolean class gives different output
有關 Boolean 的方法使用?
Boolean.parseBoolean(“1”) = false…?