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

解决方案 »

  1.   

    jdk源码private static boolean toBoolean(String name) { 
        return ((name != null) && name.equalsIgnoreCase("true"));
    }
      

  2.   

    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
      

  3.   

    正解还是多看jdk源码吧,源码在手,天下我有
      

  4.   

    按住Ctrl,点击parseBoolean,直接查看对应源码。