关于Boolean对象的compareTo方法的一些疑惑

我是个伪iter 2014-10-20 10:35:01
话不多说直接进主题
jdk1.7 eclipse luna
关于compareTo方法的api文档解释是:
如果对象与参数表示的布尔值相同,则返回零;如果此对象表示 true,参数表示 false,则返回一个正值;如果此对象表示 false,参数表示 true,则返回一个负值
java代码 System.out.println("new
Boolean(true).compareTo(true):"+new Boolean(true).compareTo(new Boolean(true)));得出结果是new Boolean(true).compareTo(true):0 这证明文档说的是正确的 但是源码如下:
...
public int compareTo(Boolean paramBoolean)
{
return compare(this.value, paramBoolean.value);
}
public static int compare(boolean paramBoolean1, boolean paramBoolean2)
{
return paramBoolean1 ? 1 : paramBoolean1 == paramBoolean2 ? 0 : -1;
}
...
按照源码如果是true和true比较结果是1为什么是0啊求解
...全文
490 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
我是个伪iter 2014-10-23
  • 打赏
  • 举报
回复
引用 5 楼 fso918 的回复:
楼主不厚道啊! Boolean.java的源码:

/**
     * 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);
    }
不知道你的这个源码从哪里看的
擦,我是用反编译工具直接反编译rt.jar包的 看来这工具还是不值得相信啊
fso918 2014-10-23
  • 打赏
  • 举报
回复
楼主不厚道啊! Boolean.java的源码:

/**
     * 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);
    }
不知道你的这个源码从哪里看的
我是个伪iter 2014-10-23
  • 打赏
  • 举报
回复
球助啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
lliiqiang 2014-10-21
  • 打赏
  • 举报
回复
true>false false<true true==true false==false 都是true,自然相等,没有小于对方,也没有大于对方,返回0.
Paul_ZJ 2014-10-21
  • 打赏
  • 举报
回复
按源码也等于0啊 public static int compare(boolean paramBoolean1, boolean paramBoolean2) { return paramBoolean1 ? 1 : paramBoolean1 == paramBoolean2 ? 0 : -1; } 假设paramBoolean1=TRUE,paramBoolean2=TRUE paramBoolean1 ? 1 : paramBoolean1这个表达式的结果是1 1 == paramBoolean2 ? 0 : -1;这个表达式的结果是0 没有问题啊
我是个伪iter 2014-10-21
  • 打赏
  • 举报
回复
引用 楼主 u010444251 的回复:
话不多说直接进主题 jdk1.7 eclipse luna 关于compareTo方法的api文档解释是: 如果对象与参数表示的布尔值相同,则返回零;如果此对象表示 true,参数表示 false,则返回一个正值;如果此对象表示 false,参数表示 true,则返回一个负值 java代码 System.out.println("new
Boolean(true).compareTo(true):"+new Boolean(true).compareTo(new Boolean(true)));得出结果是new Boolean(true).compareTo(true):0 这证明文档说的是正确的 但是源码如下: ... public int compareTo(Boolean paramBoolean) { return compare(this.value, paramBoolean.value); } public static int compare(boolean paramBoolean1, boolean paramBoolean2) { return paramBoolean1 ? 1 : paramBoolean1 == paramBoolean2 ? 0 : -1; } ... 按照源码如果是true和true比较结果是1为什么是0啊求解
对于true和true比较时 jdk1.7 true ? 1 : true == true ? 0 : -1 这个结果是1我是eclipse luna版跑了下的 他的执行顺序应该是(true ? 1 : (true == true ? 0 : -1))这样的

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧