jdk自学笔记:Boolean源码分析
程序员文章站
2022-07-14 17:27:44
...
Boolean是基本类型boolean的包装类
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));
}
- parseBoolean(String s):boolean 字符串参数不是 null并且等于“true”(忽略大小写)为ture 否则为false,构造方法实际就是调这个方法
public static boolean parseBoolean(String s) {
return ((s != null) && s.equalsIgnoreCase("true"));
}
- booleanValue():boolean 返回当前Boolean的boolean类型
public boolean booleanValue() {
return value;
}
- valueOf
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
public static Boolean valueOf(String s) {
return parseBoolean(s) ? TRUE : FALSE;
}
- toString两个方法 一个静态方法一个非静态方法
public static String toString(boolean b) {
return b ? "true" : "false";
}
public String toString() {
return value ? "true" : "false";
}
- hashCode 一个静态方法一个非静态方法
@Override
public int hashCode() {
return Boolean.hashCode(value);
}
public static int hashCode(boolean value) {
return value ? 1231 : 1237;
}
- equals(Object obj):boolean
public boolean equals(Object obj) {
if (obj instanceof Boolean) {
return value == ((Boolean)obj).booleanValue();
}
return false;
}
- compareTo
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);
}
上一篇: Jfinal框架中使用Token验证
下一篇: JDK源码-Boolean类