In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to analyze the JDK source code Java.lang.Boolean analysis, the content is very detailed, interested friends can refer to, hope to be helpful to you.
The study of JDK source code (jdk 1.5b2) is a large category, so today we begin to dissect Java.lang.Boolean.
First of all, let's take a look at the Java.lang.Boolean code in the JDK source code, excluding all methods and static variables. The core code of Java.lang.Boolean is as follows:
Public final class Boolean implements java.io.Serializable,Comparable {private final boolean value;}
Obviously, all member variables are of type final and must be immutable class, which is the same as String. Once the constructor is finished, the state of the instance cannot be changed.
There are two constructors for Java.lang.Boolean in the JDK source code:
Public Boolean (boolean value) {this.value = value;} public Boolean (String s) {this (toBoolean (s));}
It's all simple, so I won't say much.
Also note that the Java.lang.Boolean class actually has only two instances of different states: one wraps true and the other wraps false,Java.lang.Boolean and immutable class, so Boolean instances in the same state in memory can be shared without having to create many instances with new. So Boolean class also provides two static variables:
Public static final Boolean TRUE = new Boolean (true); public static final Boolean FALSE = new Boolean (false)
These two variables are instantiated when Class Loader loads and are declared as final and cannot point to other instances.
These two static variables are provided to allow developers to use them directly instead of new one Boolean at a time, which saves memory and avoids the time cost of creating a new instance.
Therefore, use the
Boolean b = Boolean.TRUE
Ratio
Boolean b = new Boolean (true)
Much better.
If you encounter the following situation:
Boolean b = new Boolean (var)
What if you have to create an Boolean instance based on a boolean variable?
It is recommended that you use the static factory method provided by Boolean:
Boolean b = Boolean.valueOf (var)
In this way, you can avoid creating new instances and look at the valueOf () static method:
Public static Boolean valueOf (boolean b) {return (b? TRUE: FALSE);}
This static factory method still returns one of the two static variables TRUE and FALSE, rather than new a Boolean. Although Java.lang.Boolean is very simple and takes up very little memory, it can be very expensive for a complex class to create an instance with new, and the factory method can be used to cache the instance easily, which is transparent to the client. Therefore, if you can use the factory method, do not use new.
There are only two states different from Boolean, Integer is also immutable class, but there are hundreds of millions of states, and it is impossible to cache all states with static instances. However, the SUN engineer did a little bit of optimization. The Integer class caches the Integer of-128to127256 states. If you use Integer.valueOf (int I), the int range passed in is exactly within this, and a static instance is returned.
The hashCode () method is strange, and the hash code of the two Boolean is 1231 and 1237, respectively. It is estimated that people who write Boolean.java have a special preference for these two numbers:
Public int hashCode () {return value? 1231: 1237;}
The equals () method is also simple, returning true only if the Object of type Boolean and value are equal:
Public boolean equals (Object obj) {if (obj instanceof Boolean) {return value = = ((Boolean) obj). BooleanValue ();} return false;} on how to parse the JDK source Java.lang.Boolean analysis is shared here, I hope the above content can be of some help to everyone, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.