1. 程式人生 > >java的boolean與string型別轉換

java的boolean與string型別轉換

Boolean.valueOf(“TRUE”)==true//此處的引數true忽略大小寫

其他都是false

直接上原始碼:

/**
 * Returns a {@code Boolean} with a value represented by the
 * specified string.  The {@code Boolean} returned represents a
 * true value if the string argument is not {@code null}
 * and is equal, ignoring case, to the string {@code 
"true"}. * * @param s a string. * @return the {@code Boolean} value represented by the string. */ public static Boolean valueOf(String s) { return toBoolean(s) ? TRUE : FALSE; }
private static boolean toBoolean(String name) {
    return ((name != null) && name.equalsIgnoreCase("true"
)); }

熟悉C語言的人都知道,C裡面0是false,其餘的非零數值都是true
java裡面也有一個方法用來實現從string型別轉到boolean型別,但是更加簡單粗暴,
Boolean.valueOf(“true”)//此處的引數true忽略大小寫
其他的值都是false,詳見上面原始碼