java.lang.Boolean 类源码解析

  • Boolean源码比较简单。

      1 public final class Boolean implements java.io.Serializable,
      2                                       Comparable<Boolean>
      3 {
      4     /**
      5      * The {@code Boolean} object corresponding to the primitive
      6      * value {@code true}.
      7      */
      8     public static final Boolean TRUE = new Boolean(true);
      9
     10     /**
     11      * The {@code Boolean} object corresponding to the primitive
     12      * value {@code false}.
     13      */
     14     public static final Boolean FALSE = new Boolean(false);
     15
     16     /**
     17      * The Class object representing the primitive type boolean.
     18      *
     19      * @since   JDK1.1
     20      */
     21     @SuppressWarnings("unchecked")
     22     public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");
     23
     24     /**
     25      * The value of the Boolean.
     26      *
     27      * @serial
     28      */
     29     private final boolean value;
     30
     31     /** use serialVersionUID from JDK 1.0.2 for interoperability */
     32     private static final long serialVersionUID = -3665804199014368530L;
     33
     34     /**
     35      * Allocates a {@code Boolean} object representing the
     36      * {@code value} argument.
     37      *
     38      * <p><b>Note: It is rarely appropriate to use this constructor.
     39      * Unless a <i>new</i> instance is required, the static factory
     40      * {@link #valueOf(boolean)} is generally a better choice. It is
     41      * likely to yield significantly better space and time performance.</b>
     42      *
     43      * @param   value   the value of the {@code Boolean}.
     44      */
     45     public Boolean(boolean value) {
     46         this.value = value;
     47     }
     48
     49     /**
     50      * Allocates a {@code Boolean} object representing the value
     51      * {@code true} if the string argument is not {@code null}
     52      * and is equal, ignoring case, to the string {@code "true"}.
     53      * Otherwise, allocate a {@code Boolean} object representing the
     54      * value {@code false}. Examples:<p>
     55      * {@code new Boolean("True")} produces a {@code Boolean} object
     56      * that represents {@code true}.<br>
     57      * {@code new Boolean("yes")} produces a {@code Boolean} object
     58      * that represents {@code false}.
     59      *
     60      * @param   s   the string to be converted to a {@code Boolean}.
     61      */
     62     public Boolean(String s) {
     63         this(parseBoolean(s));
     64     }
     65
     66     /**
     67      * Parses the string argument as a boolean.  The {@code boolean}
     68      * returned represents the value {@code true} if the string argument
     69      * is not {@code null} and is equal, ignoring case, to the string
     70      * {@code "true"}. <p>
     71      * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
     72      * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
     73      *
     74      * @param      s   the {@code String} containing the boolean
     75      *                 representation to be parsed
     76      * @return     the boolean represented by the string argument
     77      * @since 1.5
     78      */
     79     public static boolean parseBoolean(String s) {
     80         return ((s != null) && s.equalsIgnoreCase("true"));
     81     }
     82
     83     /**
     84      * Returns the value of this {@code Boolean} object as a boolean
     85      * primitive.
     86      *
     87      * @return  the primitive {@code boolean} value of this object.
     88      */
     89     public boolean booleanValue() {
     90         return value;
     91     }
     92
     93     /**
     94      * Returns a {@code Boolean} instance representing the specified
     95      * {@code boolean} value.  If the specified {@code boolean} value
     96      * is {@code true}, this method returns {@code Boolean.TRUE};
     97      * if it is {@code false}, this method returns {@code Boolean.FALSE}.
     98      * If a new {@code Boolean} instance is not required, this method
     99      * should generally be used in preference to the constructor
    100      * {@link #Boolean(boolean)}, as this method is likely to yield
    101      * significantly better space and time performance.
    102      *
    103      * @param  b a boolean value.
    104      * @return a {@code Boolean} instance representing {@code b}.
    105      * @since  1.4
    106      */
    107     public static Boolean valueOf(boolean b) {
    108         return (b ? TRUE : FALSE);
    109     }
    110
    111     /**
    112      * Returns a {@code Boolean} with a value represented by the
    113      * specified string.  The {@code Boolean} returned represents a
    114      * true value if the string argument is not {@code null}
    115      * and is equal, ignoring case, to the string {@code "true"}.
    116      *
    117      * @param   s   a string.
    118      * @return  the {@code Boolean} value represented by the string.
    119      */
    120     public static Boolean valueOf(String s) {
    121         return parseBoolean(s) ? TRUE : FALSE;
    122     }
    123
    124     /**
    125      * Returns a {@code String} object representing the specified
    126      * boolean.  If the specified boolean is {@code true}, then
    127      * the string {@code "true"} will be returned, otherwise the
    128      * string {@code "false"} will be returned.
    129      *
    130      * @param b the boolean to be converted
    131      * @return the string representation of the specified {@code boolean}
    132      * @since 1.4
    133      */
    134     public static String toString(boolean b) {
    135         return b ? "true" : "false";
    136     }
    137
    138     /**
    139      * Returns a {@code String} object representing this Boolean‘s
    140      * value.  If this object represents the value {@code true},
    141      * a string equal to {@code "true"} is returned. Otherwise, a
    142      * string equal to {@code "false"} is returned.
    143      *
    144      * @return  a string representation of this object.
    145      */
    146     public String toString() {
    147         return value ? "true" : "false";
    148     }
    149
    150     /**
    151      * Returns a hash code for this {@code Boolean} object.
    152      *
    153      * @return  the integer {@code 1231} if this object represents
    154      * {@code true}; returns the integer {@code 1237} if this
    155      * object represents {@code false}.
    156      */
    157     @Override
    158     public int hashCode() {
    159         return Boolean.hashCode(value);
    160     }
    161
    162     /**
    163      * Returns a hash code for a {@code boolean} value; compatible with
    164      * {@code Boolean.hashCode()}.
    165      *
    166      * @param value the value to hash
    167      * @return a hash code value for a {@code boolean} value.
    168      * @since 1.8
    169      */
    170     public static int hashCode(boolean value) {
    171         return value ? 1231 : 1237;
    172     }
    173
    174    /**
    175      * Returns {@code true} if and only if the argument is not
    176      * {@code null} and is a {@code Boolean} object that
    177      * represents the same {@code boolean} value as this object.
    178      *
    179      * @param   obj   the object to compare with.
    180      * @return  {@code true} if the Boolean objects represent the
    181      *          same value; {@code false} otherwise.
    182      */
    183     public boolean equals(Object obj) {
    184         if (obj instanceof Boolean) {
    185             return value == ((Boolean)obj).booleanValue();
    186         }
    187         return false;
    188     }
    189
    190     /**
    191      * Returns {@code true} if and only if the system property
    192      * named by the argument exists and is equal to the string
    193      * {@code "true"}. (Beginning with version 1.0.2 of the
    194      * Java<small><sup>TM</sup></small> platform, the test of
    195      * this string is case insensitive.) A system property is accessible
    196      * through {@code getProperty}, a method defined by the
    197      * {@code System} class.
    198      * <p>
    199      * If there is no property with the specified name, or if the specified
    200      * name is empty or null, then {@code false} is returned.
    201      *
    202      * @param   name   the system property name.
    203      * @return  the {@code boolean} value of the system property.
    204      * @throws  SecurityException for the same reasons as
    205      *          {@link System#getProperty(String) System.getProperty}
    206      * @see     java.lang.System#getProperty(java.lang.String)
    207      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
    208      */
    209     public static boolean getBoolean(String name) {
    210         boolean result = false;
    211         try {
    212             result = parseBoolean(System.getProperty(name));
    213         } catch (IllegalArgumentException | NullPointerException e) {
    214         }
    215         return result;
    216     }
    217
    218     /**
    219      * Compares this {@code Boolean} instance with another.
    220      *
    221      * @param   b the {@code Boolean} instance to be compared
    222      * @return  zero if this object represents the same boolean value as the
    223      *          argument; a positive value if this object represents true
    224      *          and the argument represents false; and a negative value if
    225      *          this object represents false and the argument represents true
    226      * @throws  NullPointerException if the argument is {@code null}
    227      * @see     Comparable
    228      * @since  1.5
    229      */
    230     public int compareTo(Boolean b) {
    231         return compare(this.value, b.value);
    232     }
    233
    234     /**
    235      * Compares two {@code boolean} values.
    236      * The value returned is identical to what would be returned by:
    237      * <pre>
    238      *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
    239      * </pre>
    240      *
    241      * @param  x the first {@code boolean} to compare
    242      * @param  y the second {@code boolean} to compare
    243      * @return the value {@code 0} if {@code x == y};
    244      *         a value less than {@code 0} if {@code !x && y}; and
    245      *         a value greater than {@code 0} if {@code x && !y}
    246      * @since 1.7
    247      */
    248     public static int compare(boolean x, boolean y) {
    249         return (x == y) ? 0 : (x ? 1 : -1);
    250     }
    251
    252     /**
    253      * Returns the result of applying the logical AND operator to the
    254      * specified {@code boolean} operands.
    255      *
    256      * @param a the first operand
    257      * @param b the second operand
    258      * @return the logical AND of {@code a} and {@code b}
    259      * @see java.util.function.BinaryOperator
    260      * @since 1.8
    261      */
    262     public static boolean logicalAnd(boolean a, boolean b) {
    263         return a && b;
    264     }
    265
    266     /**
    267      * Returns the result of applying the logical OR operator to the
    268      * specified {@code boolean} operands.
    269      *
    270      * @param a the first operand
    271      * @param b the second operand
    272      * @return the logical OR of {@code a} and {@code b}
    273      * @see java.util.function.BinaryOperator
    274      * @since 1.8
    275      */
    276     public static boolean logicalOr(boolean a, boolean b) {
    277         return a || b;
    278     }
    279
    280     /**
    281      * Returns the result of applying the logical XOR operator to the
    282      * specified {@code boolean} operands.
    283      *
    284      * @param a the first operand
    285      * @param b the second operand
    286      * @return  the logical XOR of {@code a} and {@code b}
    287      * @see java.util.function.BinaryOperator
    288      * @since 1.8
    289      */
    290     public static boolean logicalXor(boolean a, boolean b) {
    291         return a ^ b;
    292     }
    293 }

原文地址:https://www.cnblogs.com/aben-blog/p/8729428.html

时间: 2024-11-05 21:47:22

java.lang.Boolean 类源码解析的相关文章

java.lang.Void类源码解析_java - JAVA

文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 在一次源码查看ThreadGroup的时候,看到一段代码,为以下: /* * @throws NullPointerException if the parent argument is {@code null} * @throws SecurityException if the current thread cannot create a * thread in the specified thread grou

Java集合---Array类源码解析

Java集合---Array类源码解析              ---转自:牛奶.不加糖 一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型:采用改进的归并排序. 1.对于基本类型源码分析如下(以int[]为例): Java对Primitive(int,float等原型数据)数组采用快速排序,对Object对象数组采用归并排序.对这一区别,sun在

java.lang.String 类源码解读

String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public final class String defined String由char[]数组实现 /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the strin

Java集合---Arrays类源码解析

一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型:采用改进的归并排序. 1.对于基本类型源码分析如下(以int[]为例): Java对Primitive(int,float等原型数据)数组采用快速排序,对Object对象数组采用归并排序.对这一区别,sun在<<The Java Tutorial>>中做出的解释如下: The sort

java.lang.Long 类源码解读

总体阅读了Long的源码,基本跟Integer类类似,所以特别全部贴出源码,直接注释进行理解. 1 // final修饰符 2 public final class Long extends Number implements Comparable<Long> { 3 /** 4 * A constant holding the minimum value a {@code long} can 5 * have, -2<sup>63</sup>. 6 */ 7 // 最

java.lang.Byte 类源码浅析

Byte 类字节,属于Number. 1 public final class Byte extends Number implements Comparable<Byte> { 2 3 /** 4 * A constant holding the minimum value a {@code byte} can 5 * have, -2<sup>7</sup>. 6 */ 7 public static final byte MIN_VALUE = -128; 8 9

There is no getter for property named &#39;*&#39; in &#39;class java.lang.String&#39;之源码分析

There is no getter for property named '*' in 'class java.lang.String',此错误之所以出现,是因为mybatis在对parameterType="String"的sql语句做了限制,假如你使用<when test="username != null">这样的条件判断时,就会出现该错误,不过今天我们来刨根问底一下. 一.错误再现 想要追本溯源,就需要错误再现,那么假设我们有这样一个sql查询

java-AbstractCollection类-源码解析

转载:原文地址 http://www.cnblogs.com/android-blogs/p/5566212.html 一.Collection接口 从<Java集合:整体结构>一文中我们知道所有的List和Set都继承自Collection接口,该接口类提供了集合最基本的方法,虽然List接口和Set等都有一些自己独有的方法,但是基本的操作类似.我们先看下Collection接口提供的方法: 总体上可以将Collection的方法分为以下几大类: 1.增加(add/addAll) 2.删除(

Java集合类库 ArrayList 源码解析

集合类库是Java的一个重大突破,方便了我们对大数据的操作.其中 Arrays 和 Collections 工具类可以帮助我们快速操作集合类库.下面对Java集合类库的源码分析是基于jdk1.7的.今天我们来看看ArrayList的底层实现原理. ArrayList的继承结构图 继承自 AbstractList 抽象类,在上层是 AbstractCollection 抽象类,直接去 AbstractCollection 类去看看. AbstractCollection 类主要实现了 Collec