深入學習java原始碼之下Boolean類
深入學習java原始碼之下Boolean類
布林類將物件中的基元型別boolean
的值進行包裝。 型別為Boolean
的物件包含一個單一欄位,其型別為boolean
。
此外,該類還提供了許多將boolean
轉換為String
和String
轉換為boolean
,以及在處理boolean
時有用的其他常量和方法。
方法摘要
靜態方法
Modifier and Type | Method and Description |
---|---|
boolean |
booleanValue() 將此 |
static int |
compare(boolean x, boolean y) 比較兩個 |
int |
compareTo(Boolean b) 將此 |
boolean |
equals(Object obj) 返回 |
static boolean |
getBoolean(String name) 返回 |
int |
hashCode() 返回此 |
static int |
hashCode(boolean value) 返回一個 |
static boolean |
logicalAnd(boolean a, boolean b) 返回將邏輯AND運算子應用於指定的 |
static boolean |
logicalOr(boolean a, boolean b) 返回將邏輯OR運算子應用於指定的 |
static boolean |
logicalXor(boolean a, boolean b) 返回將邏輯XOR運算子應用於指定的 |
static boolean |
parseBoolean(String s) 將字串引數解析為布林值。 |
String |
toString() 返回一個 |
static String |
toString(boolean b) 返回一個 |
static Boolean |
valueOf(boolean b) 返回一個 |
static Boolean |
valueOf(String s) 返回一個 |
介面方法
Modifier and Type | Method and Description |
---|---|
boolean |
booleanValue() 將此 |
int |
compareTo(Boolean b) 將此 |
boolean |
equals(Object obj) 返回 |
int |
hashCode() 返回此 |
String |
toString() 返回一個 |
java原始碼
public final class Boolean implements java.io.Serializable,
Comparable<Boolean>
{
public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
@SuppressWarnings("unchecked")
public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");
private final boolean value;
private static final long serialVersionUID = -3665804199014368530L;
public Boolean(boolean value) {
this.value = value;
}
public Boolean(String s) {
this(parseBoolean(s));
}
public static boolean parseBoolean(String s) {
return ((s != null) && s.equalsIgnoreCase("true"));
}
public boolean booleanValue() {
return value;
}
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
public static Boolean valueOf(String s) {
return parseBoolean(s) ? TRUE : FALSE;
}
public static String toString(boolean b) {
return b ? "true" : "false";
}
public String toString() {
return value ? "true" : "false";
}
@Override
public int hashCode() {
return Boolean.hashCode(value);
}
public static int hashCode(boolean value) {
return value ? 1231 : 1237;
}
public boolean equals(Object obj) {
if (obj instanceof Boolean) {
return value == ((Boolean)obj).booleanValue();
}
return false;
}
public static boolean getBoolean(String name) {
boolean result = false;
try {
result = parseBoolean(System.getProperty(name));
} catch (IllegalArgumentException | NullPointerException e) {
}
return result;
}
public int compareTo(Boolean b) {
return compare(this.value, b.value);
}
public static int compare(boolean x, boolean y) {
return (x == y) ? 0 : (x ? 1 : -1);
}
public static boolean logicalAnd(boolean a, boolean b) {
return a && b;
}
public static boolean logicalOr(boolean a, boolean b) {
return a || b;
}
public static boolean logicalXor(boolean a, boolean b) {
return a ^ b;
}
}