关于Boolean对象的compareTo步骤的一些疑惑
关于Boolean对象的compareTo方法的一些疑惑
话不多说直接进主题
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啊求解
------解决思路----------------------
楼主不厚道啊!
Boolean.java的源码:
不知道你的这个源码从哪里看的
话不多说直接进主题
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啊求解
------解决思路----------------------
楼主不厚道啊!
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);
}
不知道你的这个源码从哪里看的