JDK source (7) -Boolean

I. Overview

This class is still Arthur van Hoff written by this man, is a package of basic data types boolean, the source of the comments said very clearly:

An object of type Boolean contains a single field whose type is boolean. There is a class attribute type is primitive boolean

This class provides a boolean boolean many methods and String mutual conversion, and provides a number of constants and methods when processing the boolean.

Second, the common method

1. The class definition is as follows:

public final class Boolean implements java.io.Serializable,Comparable<Boolean>

2. The five properties

    //对应于基本类型boolean中的true
    public static final Boolean TRUE = new Boolean(true);
    
    public static final Boolean FALSE = new Boolean(false);

    // 表示基本布尔型的类对象
    public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");

    //The value of the Boolean.
    private final boolean value;

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -3665804199014368530L;

3. constructor:

    public Boolean(boolean value) {
        this.value = value;
    }

However, this construction method is generally the case is rarely used, is using his static factory method valueOf (boolean). This factory method has better performance advantage when space. In addition, a configuration also provides a string parameter:

    public Boolean(String s) {
        this(parseBoolean(s));
    }

In the method of the specified logical parseBoolean,

    public static boolean parseBoolean(String s) {
        //传入的字符串非空并且为忽略大小写的true时,返回true,否则返回false
        return ((s != null) && s.equalsIgnoreCase("true"));
    }

Static constructors plants (official documentation is so called), good performance:

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

    //String参数的
    public static Boolean valueOf(String s) {
        return parseBoolean(s) ? TRUE : FALSE;
    }

4.hashCode method, one 1231, one 1237

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

5.equals

    public boolean equals(Object obj) {
        if (obj instanceof Boolean) {
            return value == ((Boolean)obj).booleanValue();
        }
        return false;
    }

6.getBoolean

It gets the name corresponding to the value of the parameter from the system parameters, if it is "true" returns true. Otherwise, throws an exception or a null pointer exception parameters.

    public static boolean getBoolean(String name) {
        boolean result = false;
        try {
            result = parseBoolean(System.getProperty(name));
        } catch (IllegalArgumentException | NullPointerException e) {
        }
        return result;
    }

7.compare

Comparison of two Boolean values ​​are equal, equal returns 0, otherwise return to the first parameter is a true, the first parameter is false -1.

Method three special points

Three logical Method:

    public static boolean logicalAnd(boolean a, boolean b) {
        return a && b;
    }

    public static boolean logicalOr(boolean a, boolean b) {
        return a || b;
    }

    public static boolean logicalXor(boolean a, boolean b) {
        return a ^ b;
    }

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_37609579/article/details/103521971