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