日期:2014-05-20  浏览次数:20685 次

小白一枚,请教关于包装类中字符串类型转换成基本数据类型的问题
包装类中字符串类型转换成基本数据类型时,参数不应该得是相应类型的字符串表示形式吗?为什么这里写123456也没有报错,而是运行时显示false呢?
java 包装类 类型转换

------解决方案--------------------
parseBoolean是包装类下Boolean下的方法,为了更明白一些,我们来看一下源代码
 /**
     * Parses the string argument as a boolean.  The <code>boolean</code> 
     * returned represents the value <code>true</code> if the string argument 
     * is not <code>null</code> and is equal, ignoring case, to the string 
     * {@code "true"}. <p>
     * Example: {@code Boolean.parseBoolean("True")} returns <tt>true</tt>.<br>
     * Example: {@code Boolean.parseBoolean("yes")} returns <tt>false</tt>.
     *
     * @param      s   the <code>String</code> containing the boolean
     *                 representation to be parsed
     * @return     the boolean represented by the string argument
     * @since 1.5
     */
    public static boolean parseBoolean(String s) {
        return toBoolean(s);
    }
源代码中很明显的解释了String类型转boolean类型的一个方法,当String的值为“true”时返回ture,当为其他字符串时返回false