【java】java boolean 源码分析

412 篇文章 483 订阅 ¥19.90 ¥99.00
本文档详细分析了Java中boolean类型的几个关键方法,包括booleanValue()、toString()、hashCode()、compareTo()和equals()。每个方法的实现原理和使用场景都有所介绍,例如booleanValue()返回对象的boolean值,toString()转换为字符串,hashCode()生成哈希值,compareTo()进行比较,而equals()则用于对象内容的比较。同时,文中也提及了扩展阅读内容,涉及AtomicBoolean的介绍与应用。
摘要由CSDN通过智能技术生成

在这里插入图片描述

1.概述

转载:jdk源码分析-------------boolean

这篇文章小编只是对boolean类型的几个方法进行一次知识梳理,大家有好的意见可以在留言区留下宝贵的意见,小编会做出相应的调整。

1.1 booleanValue()

   /**
     * 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;
    }

这个方法返回的是当前这个boolean类型对象的内容。

boolean b=true;
System.out.println(b.booleanValue());
----------------------------------------
true

1.2 toString()

/**
 * 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";
}

这个方法返回类型是string,返回的是boolean对象的内容

boolean b=false;
System.out.println(b.booleanValue());
----------------------------------------
false

1.3 hashCode()

   /**
     * 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;
    }

当调用hashCode返回的是true或者false的哈希值。在hashCode这个方法中通过三目表达式表现出来。

1.4 compareTo()

 /**
     * Compares this {@code Boolean} instance with another.
     *
     * @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);
    }

调用这个方法时会拿当前对象和要比较的对象value进行比较。compare方法最终定义只能返回1或者-1。

1.5 equals()

   /**
     * 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.
     *
     * @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;
    }

如果当前对象时boolean的实例,就返回这个对象的内容,否则返回false

N.扩展阅读

AtomicBoolean介绍与使用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值