JDK1.8源码学习--lang包(Boolean)

前言


月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂)

央是一片海洋,海乃百川,代表着一块海绵(吸纳万物)

泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出)

月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容

希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄!
 

一.Boolean的作用:

  • Boolean是原始类型boolean的包装类,为了更方便的去代替boolean去做一些操作
  • Boolean中包含了boolean的字段,以及true和false值,此外,还提供了很多方法去处理boolean,比如将boolean转化为String,把String转化为boolean的方法

二.Boolean的类图: 

 

Boolean是有实现两个接口:

                a).一个Comparable,这个接口对实现他的每个对象都可按照一定的规则来进行排序,详情请点击下面链接

                  ......(假装这个是链接,以后补充)

                b).一个Secializable,这个接口是可为实现的对象进行序列化,详情请点击下面链接
                  ......(假装这个是链接,以后补充)

三.成员变量:  

  /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code true}.
     * 此成员变量维护了原始类型的true值
     */
    public static final Boolean TRUE = new Boolean(true);

    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code false}.
     * 此成员变量维护了原始类型的false值
     */
    public static final Boolean FALSE = new Boolean(false);

    /**
     * The Class object representing the primitive type boolean.
     *表示基本类型布尔值的Class对象
     * @since   JDK1.1
     */
    @SuppressWarnings("unchecked")
    public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");

    /**
     * The value of the Boolean.
     *  初始化时会赋值给value(要么为true,要么false)
     * @serial
     */
    private final boolean value;

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

四.构造方法:  

  /**
     * Allocates a {@code Boolean} object representing the
     * {@code value} argument.
     * 直接通过boolean类型赋值
     *  将传入的参数value赋值给成员变量value(要么为true,要么false)
     * <p><b>Note: It is rarely appropriate to use this constructor.
     * Unless a <i>new</i> instance is required, the static factory
     * {@link #valueOf(boolean)} is generally a better choice. It is
     * likely to yield significantly better space and time performance.</b>
     *  注明:很少去使用这个构造函数,除非是有要求,但是静态工厂方法(valueOf)会更好,
     *  因为它会有更明显的空间和时间效率
     * @param   value   the value of the {@code Boolean}.
     */
    public Boolean(boolean value) {
        this.value = value;
    }

    /**
     * Allocates a {@code Boolean} object representing the value
     * {@code true} if the string argument is not {@code null}
     * and is equal, ignoring case, to the string {@code "true"}.
     * Otherwise, allocate a {@code Boolean} object representing the
     * value {@code false}. Examples:<p>
     * {@code new Boolean("True")} produces a {@code Boolean} object
     * that represents {@code true}.<br>
     * {@code new Boolean("yes")} produces a {@code Boolean} object
     * that represents {@code false}.

     * 将String(不能为空) 的值解析为true和false
     * 只有输入"true"时为true,其他都会false
     * 
     *
     * @param   s   the string to be converted to a {@code Boolean}.
     */
    public Boolean(String s) {
        this(parseBoolean(s));
    }

五.成员方法:  

                a.parseBoolean(String s)

                        将字符串解析为一个布尔值

  /**
     * Parses the string argument as a boolean.  The {@code boolean}
     * returned represents the value {@code true} if the string argument
     * is not {@code null} and is equal, ignoring case, to the string
     * {@code "true"}. <p>
     * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
     * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
     *  将字符串解析为一个布尔值,如果输入的字符串是true(忽略大小写),则会放回true
     *  其他情况都是false
     * @param      s   the {@code String} 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 ((s != null) && s.equalsIgnoreCase("true"));
    }

                b.booleanValue()

                返回boolean的值

/**
     * 返回成员变量value(布尔类型)的值
     * Returns the value of this {@code Boolean} object as a boolean
     * primitive.
     *
     * @return  the primitive {@code boolean} value of this object.
     */
    public boolean booleanValue() {
        return value;
    }

                c.valueOf(boolean b)

                     返回包装类Boolean(true或false,取决于boolean)

/**
     * Returns a {@code Boolean} instance representing the specified
     * {@code boolean} value.  If the specified {@code boolean} value
     * is {@code true}, this method returns {@code Boolean.TRUE};
     * if it is {@code false}, this method returns {@code Boolean.FALSE}.
     * If a new {@code Boolean} instance is not required, this method
     * should generally be used in preference to the constructor
     * {@link #Boolean(boolean)}, as this method is likely to yield
     * significantly better space and time performance.
     *  1.静态工厂方法,放回一个封装的boolean值Boolean
     *  2.如果是true,则返回Boolean(true)
     *  3.如果是false,则返回Boolean(false)
     *  4.这个方法更有效率,更省空间
     * @param  b a boolean value.
     * @return a {@code Boolean} instance representing {@code b}.
     * @since  1.4
     */
    public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

                d.valueOf(String s)

                将字符串转化为boolean的包装类

/**
     * 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"}.
     *  将字符串转化为boolean的包装类Boolean
     *  判断依据:将字符串解析为一个布尔值,如果输入的字符串是true(忽略大小写),则会放回true
     * 其他情况都是false
     * @param   s   a string.
     * @return  the {@code Boolean} value represented by the string.
     */
    public static Boolean valueOf(String s) {
        return parseBoolean(s) ? TRUE : FALSE;
    }

                e.toString(boolean b)

                        将传入的boolean转化为"true"或"false"字符串

/**
     * Returns a {@code String} object representing the specified
     * boolean.  If the specified boolean is {@code true}, then
     * the string {@code "true"} will be returned, otherwise the
     * string {@code "false"} will be returned.
     *  将传进的boolean转化为String字符串("true"或"false")
     *  
     * @param b the boolean to be converted
     * @return the string representation of the specified {@code boolean}
     * @since 1.4
     */
    public static String toString(boolean b) {
        return b ? "true" : "false";
    }

                f.toString()

                        将原本内部封装的value转化为"true"或"false"字符串

 /**
     * Returns a {@code String} object representing this Boolean's
     * value.  If this object represents the value {@code true},
     * a string equal to {@code "true"} is returned. Otherwise, a
     * string equal to {@code "false"} is returned.
     *
     * @return  a string representation of this object.
     */
    public String toString() {
        return value ? "true" : "false";
    }

                g.hashCode()

                        计算Boolean的哈希值,内部会调用Boolean.hashCode(),为true就是1231,为false就是1237

/**
     * Returns a hash code for this {@code Boolean} object.
     *
     * @return  the integer {@code 1231} if this object represents
     * {@code true}; returns the integer {@code 1237} if this
     * object represents {@code false}.
     */
    @Override
    public int hashCode() {
        return Boolean.hashCode(value);
    }

    /**
     * Returns a hash code for a {@code boolean} value; compatible with
     * {@code Boolean.hashCode()}.
     *
     * @param value the value to hash
     * @return a hash code value for a {@code boolean} value.
     * @since 1.8
     */
    public static int hashCode(boolean value) {
        return value ? 1231 : 1237;
    }

                h.equals(Object obj)

                判断传入的对象的boolean值是否与内部value的boolean值相等

/**
     * Returns {@code true} if and only if the argument is not
     * {@code null} and is a {@code Boolean} object that
     * represents the same {@code boolean} value as this object.
     *  将传入的非空对象与value值比对,如果是Boolean值类型,则比对是否相等,相等为true,不相等为false
    *   若非空对象Boolean类型,则直接返回false
     * @param   obj   the object to compare with.
     * @return  {@code true} if the Boolean objects represent the
     *          same value; {@code false} otherwise.
     */
    public boolean equals(Object obj) {
        if (obj instanceof Boolean) {
            return value == ((Boolean)obj).booleanValue();
        }
        return false;
    }

                i.getBoolean(String name)

                        判断以参数name命名的系统属性是否存在且等于"true"字符串时才返回true,否则返回"false"

/**
     * Returns {@code true} if and only if the system property
     * named by the argument exists and is equal to the string
     * {@code "true"}. (Beginning with version 1.0.2 of the
     * Java<small><sup>TM</sup></small> platform, the test of
     * this string is case insensitive.) A system property is accessible
     * through {@code getProperty}, a method defined by the
     * {@code System} class.
     * <p>
     *  1.此方法是返回某个系统的属性是否存在,返回true的情况只有当前以参数命名的系统属性存在,并且
     *  等于字符串"true"的时候,才会放回true
     * If there is no property with the specified name, or if the specified
     * name is empty or null, then {@code false} is returned.
     *  2.其他情况都会返回flase
     * @param   name   the system property name.
     * @return  the {@code boolean} value of the system property.
     * @throws  SecurityException for the same reasons as
     *          {@link System#getProperty(String) System.getProperty}
     * @see     java.lang.System#getProperty(java.lang.String)
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
     */
    public static boolean getBoolean(String name) {
        boolean result = false;
        try {
            result = parseBoolean(System.getProperty(name));
        } catch (IllegalArgumentException | NullPointerException e) {
        }
        return result;
    }

                j.compareTo(Boolean b)

                        比较传入的Booleand的boolean值是否相等,内部调用compare(boolean x,boolean y)方法进行比较


    /**
     * Compares this {@code Boolean} instance with another.
     *  1.比较传入的Boolean是否与当前相等
     *  2.相等传0,返回正值表明,传入的参数为false,返回负值表示传入的参数为true
     *  3.该参数传null,会报NullPointerException
     * @param   b the {@code Boolean} instance to be compared
     * @return  zero if this object represents the same boolean value as the
     *          argument; a positive value if this object represents true
     *          and the argument represents false; and a negative value if
     *          this object represents false and the argument represents true
     * @throws  NullPointerException if the argument is {@code null}
     * @see     Comparable
     * @since  1.5
     */
    public int compareTo(Boolean b) {
        return compare(this.value, b.value);
    }

    /**
     * Compares two {@code boolean} values.
     * The value returned is identical to what would be returned by:
     * <pre>
     *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
     * </pre>
     *
     * @param  x the first {@code boolean} to compare
     * @param  y the second {@code boolean} to compare
     * @return the value {@code 0} if {@code x == y};
     *         a value less than {@code 0} if {@code !x && y}; and
     *         a value greater than {@code 0} if {@code x && !y}
     * @since 1.7
     */
    public static int compare(boolean x, boolean y) {
        return (x == y) ? 0 : (x ? 1 : -1);
    }

                l.逻辑判断,这个基本不怎么用,了解即可

  /**
     * Returns the result of applying the logical AND operator to the
     * specified {@code boolean} operands.
     *
     * @param a the first operand
     * @param b the second operand
     * @return the logical AND of {@code a} and {@code b}
     * @see java.util.function.BinaryOperator
     * @since 1.8
     * 逻辑判断:
     *  当且仅当传入的双方都是true时,才会返回true
     *   其他情况都为false
     */
    public static boolean logicalAnd(boolean a, boolean b) {
        return a && b;
    }

    /**
     * Returns the result of applying the logical OR operator to the
     * specified {@code boolean} operands.
     *
     * @param a the first operand
     * @param b the second operand
     * @return the logical OR of {@code a} and {@code b}
     * @see java.util.function.BinaryOperator
     * @since 1.8
     * 逻辑判断:
     *      *  当且仅当传入的双方都是false时,才会返回false
     *      *   其他情况都为true
     */
    public static boolean logicalOr(boolean a, boolean b) {
        return a || b;
    }

    /**
     * Returns the result of applying the logical XOR operator to the
     * specified {@code boolean} operands.
     *
     * @param a the first operand
     * @param b the second operand
     * @return  the logical XOR of {@code a} and {@code b}
     * @see java.util.function.BinaryOperator
     * @since 1.8
     *  逻辑判断:
     *        当且仅当传入的双方都不同时,才会返回true
     *        其他情况都为false
     */
    public static boolean logicalXor(boolean a, boolean b) {
        return a ^ b;
    }

六.总结:  

        包装类Boolean,确实用的很少,虽然其内部源码很简单,但是存在必有其有效性,相信在未来,会不断跟此类打交道.

        fighting!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值