Boolean Source Code Analysis

Source: Internet
Author: User

Constants inside the Boolean class:

Boolean.true: This is the constructor that calls Boolean and creates a new Boolean object, so TRUE is a Boolean type. To avoid creating a new Boolean object each time, you can pass Boolean b = boolean.true;

public static Finalboolean true = new Boolean (true);

Boolean.false: This is the constructor that calls Boolean and creates a new Boolean object, so FALSE is a Boolean type.

public static Finalboolean false = new Boolean (false);

The type of Boolean.TYP:TYPE is class<boolean> by calling Calss<t> is a final class with a generic type

Here, the class object is returned by calling the Getprimitiveclass (String name) method of class's static native

public static Final class<boolean>type = Class.getprimitiveclass ("Boolean");

Two constructed classes of a Boolean

The first parameter is the Boolean type.

Public Boolean (Boolean value) {

This.value = value;

}

The second parameter is the string type, when this place calls the ToBoolean (string s) method, ToBoolean (Strin G s) is a private method, and the returned type is a Boolean type. This place only returns True when the string is "true" (regardless of its capitalization), and all other times it returns false.

Public Boolean (String s) {

This (ToBoolean (s));

}

private static Boolean ToBoolean (Stringname) {

Return ((name = null) &&name.equalsignorecase ("true"));

}

This is like the Integer.parseint (string s) method, which converts a string into an object of its own type, which is converted to a Boolean type, where the same constructor is called ToBoolean (Strin G s) method

public static Boolean Parseboolean (Strings) {

Return ToBoolean (s);

}

This method is a unboxing operation that converts a Boolean type value to a Boolean type

public Boolean booleanvalue () {

return value;

}

This method is like an integer valueof (int i) method, which also returns a Boolean instance, which was created early, and this place is a boxing operation. It's just a pre-created instance of the cache. This is a static factory method that creates a Boolean object

public static Boolean valueOf (Boolean b) {

return (b? True:false);

}

Convert a Boolean type to a string type

public static String toString (Boolean b) {

Return b? "True": "false";

}

Convert a value of type Boolean to a string type

Public String toString () {

return value? "True": "false";

}

Converts a Boolean value to the corresponding hashcode

Here's why 1231,1237, these two are already large enough primes. The first is not a composite number, for a hash table or hash map, it is prone to conflict, if the smaller prime numbers, may appear uneven distribution

public int hashcode () {

return value? 1231:1237;

}

Compares two Boolean objects for equality, compares them by converting to a Boolean type, and finally returns a Boolean type of data.

public boolean equals (Object obj) {

if (obj instanceof Boolean) {

return value = = ((Boolean) obj). Booleanvalue ();

}

return false;

}

Determines that the system attribute exists and is the string "true", and returns False if it is not

public static Boolean Getboolean (Stringname) {

Boolean result = false;

try {

result = ToBoolean (System.getproperty (name));

} catch (IllegalArgumentException e) {

} catch (NullPointerException e) {

}

return result;

}

Compares two Boolean variables, if equal returns 0, if not equal, the first variable is true, returns 1, and the first variable is false-returns 1

public static int Compare (Boolean X,boolean y) {

return (x = = y)? 0: (x? 1:-1);

}

Compare two Boolean variables, call the Compare (Booleanx, Boolean y) method

public int CompareTo (Boolean b) {

Return compare (This.value, b.value);

}

Boolean Source Code Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.