Java.lang.Boolean Source Code Research

zhaozj2021-02-12  155

http://blog.9cbs.net/asklxf/archive/2004/06/23/23589.aspx

I have nothing to do, start to study the JDK source code (JDK 1.5 B2), first find a simplest java.lang.Boolean start anatomy. Since the level is limited, there is a lot of mistakes, please refer to you! First we eliminate all methods and static variables, Boolean's core code is as follows:

Public Final Class Boolean Implements Java.io.Serializable, Comparable {Private Final Boolean Value;

Obviously, all member variables are all type, must be Immutable Class, this Boolean and String, once the constructor is executed, the status of the instance can no longer change again. Boolean's constructor has two:

Public Boolean (Boolean Value) {this.Value = Value;} public boolean (string s) {this (toBoolean (s));}

It's very simple to say. Also note that the Boolean class actually has only two different states: a packaging true, a packaging false, Boolean is an Immutable class, so the Boolean instance of the same state in memory is completely shared, and many instances are created with NEW. Therefore, Boolean Class also provides two static variables:

Public static final boolean true = new boolean; public static final boolean false = new boolean (false);

These two variables are instantiated when the Class Loader is loaded, and the declaration is Final, which can no longer point to other instances. Providing these two static variables are to let developers directly using these two variables instead of a Boolean every time, this saves memory and avoiding time overhead for creating a new instance. So use Boolean B = Boolean.true; more than Boolean B = New Boolean (TRUE); If you encounter the following situation: Boolean B = New Boolean (var); be sure to create a Boolean instance to create a Boolean instance? It is recommended that you use the static factory method provided by Boolean: Boolean B = Boolean.Valueof (var); this can avoid creating a new instance, do not believe Valueof () static method:

Public Static Boolean Valueof (Boolean B) {RETURN (B? True: false);

This static factory method returns to one of two static variables true and false, rather than New a boolean. Although Boolean is very simple, it is very much more memory, but a complex class created instance is very large, and the use of factory methods can easily implement cache instances, which is transparent to the client. Therefore, you can use new. There are only two states of Boolean, and Integer is also Immutable Class, but there are hundreds of millions of species, and it is impossible to cache all states with a static instance. However, Sun's engineers still have a little optimization, and the Integer class buys Integer in the 256 state of -128 to 127. If INTEGER.VALUEOF (INT i), the incoming int range is in this, returns a static instance . The Hashcode () method is very strange, and the two Boolean's Hash Code is 1231 and 1237, respectively. It is estimated that the people written by Boolean.java have special preferences for these two numbers: public int hashcode () {RETURN VALUE? 1231: 1237;

The equals () method is also very simple, only the boolean type Object and Value is equivalent to return TRUE:

Public Boolean Equals (Object Obj) {if (Obj InstanceOf Boolean) {Return Value == ((Boolean) Obj) .BooleanValue ();} Return False;

转载请注明原文地址:https://www.9cbs.com/read-7499.html

New Post(0)