您的位置:首页 > 编程语言 > Java开发

03 java.lang.Boolean

2015-08-20 21:13 686 查看

Boolean

2015.01.10
By 970655147


这就是传中中值域只有两个的Boolean, 这个封装类是挺简单的

start ->

声明

, 大家可以看看注释

/**
* The Boolean class wraps a value of the primitive type
* {@code boolean} in an object. An object of type
* {@code Boolean} contains a single field whose type is
* {@code boolean}.
* <p>
* In addition, this class provides many methods for
* converting a {@code boolean} to a {@code String} and a
* {@code String} to a {@code boolean}, as well as other
* constants and methods useful when dealing with a
* {@code boolean}.
*
* @author  Arthur van Hoff
* @since   JDK1.0
*/
public final class Boolean implements java.io.Serializable,Comparable<Boolean>


Boolean的属性(们)

:

/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);

/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);

/**
* The Class object representing the primitive type boolean.
*
* @since   JDK1.1
*/

public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
/**
* The value of the Boolean.
*
* @serial
private final boolean value;

/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -3665804199014368530L;


Boolean. parseBoolean(String s)

public static boolean parseBoolean(String s) {
return toBoolean(s);
}

private static boolean toBoolean(String name) {
// 如果字符串不为null并且不为“true/TRUE”就返回true
return ((name != null) && name.equalsIgnoreCase("true"));
}


Boolean. booleanValue()

public boolean booleanValue() {
// 返回封装的boolean值
return value;
}


Boolean. valueOf(boolean b)

public static Boolean valueOf(boolean b) {
// 如果b是true 返回TRUE对象 否则返回FALSE对象
return (b ? TRUE : FALSE);
}

public static Boolean valueOf(String s) {
// 如果s是true 则返回TRUE对象 否则返回FALSE对象
return toBoolean(s) ? TRUE : FALSE;
}


Boolean. toString()

public static String toString(boolean b) {
// 如果b是true 返回“true” 否则返回“false”
return b ? "true" : "false";
}

public String toString() {
// 如果封装的value是true 返回“true” 否则返回“false”
return value ? "true" : "false";
}


Boolean. hashCode()

public int hashCode() {
// 如果封装的value是true hash值是1231 否则返回hash值是1237
return value ? 1231 : 1237;
}


Boolean. equals(Object obj)

public boolean equals(Object obj) {
// RTTI 判断obj是否是Boolean或者是其子类
if (obj instanceof Boolean) {
return value == ((Boolean)obj).booleanValue();
}
return false;
}


Boolean. getBoolean(String name)

// 获取系统属性的Boolean值
public static boolean getBoolean(String name) {
boolean result = false;
try {
result = toBoolean(System.getProperty(name));
} catch (IllegalArgumentException e) {
} catch (NullPointerException e) {
}
return result;
}


Boolean. compareTo(Boolean b)

public int compareTo(Boolean b) {
return compare(this.value, b.value);
}

public static int compare(boolean x, boolean y) {
// 如果x==y 返回0 否则如果x为ture返回1 x为false返回-1
return (x == y) ? 0 : (x ? 1 : -1);
}


-> end

哈哈, 只有两个值域, 果然内容就没有Integer那么丰富了…

资源下载 : http://download.csdn.net/detail/u011039332/9029769

注 : 因为作者的水平有限,必然可能出现一些bug, 所以请大家指出!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java-源码