Boolean

Boolean.class 位于packge java.lang,实现 java.io.Serializable,Comparable<Boolean>接口。包含内置包装对象。

 public static final Boolean TRUE = new Boolean(true);

    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code false}.
     */
    public static final Boolean FALSE = new Boolean(false);

由下可以看出,parseBoolean(String s),当且仅当是不区分大小写的"true"时返回true

public static boolean parseBoolean(String s) {
        return ((s != null) && s.equalsIgnoreCase("true"));
    }

以下这个代码返回的true。

System.out.println(Boolean.parseBoolean("12312") == Boolean.FALSE);

任意的valueOf方法都返回静态的包装类

public static Boolean valueOf(String s) {
        return parseBoolean(s) ? TRUE : FALSE;
    }
 public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

Boolean的hashCode是固定值

public static int hashCode(boolean value) {
        return value ? 1231 : 1237;
    }

注意:getBoolean用户测试系统属性的值是不是true,如下

    System.setProperty("12312312", "true");
        System.out.println(Boolean.getBoolean("12312312"));

  public static boolean getBoolean(String name) {

        boolean result = false;

        try {

            result = parseBoolean(System.getProperty(name));

        } catch (IllegalArgumentException | NullPointerException e) {

        }

        return result;

    }

compare方法,

1.x和y都是false或true,返回0,即相等

2.如果x是true,返回1

3.如果x是false,返回0

也即,true用数字1表示,false用数字0表示。

public static int compare(boolean x, boolean y) {
        return (x == y) ? 0 : (x ? 1 : -1);
    }
原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/11137593.html