In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "Why not recommend boolean type variables to use isXXX", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Why not recommend boolean type variables to use isXXX" bar!
Background: boolean and Boolean data are often used in our work. The former is the basic data type and the latter is the wrapper class. Why not use isXXX to name it? Is it better to use basic types of data or packaging classes?
Example:
1. Other non-boolean types private String isHot;public String getIsHot () {return isHot;} 2.boolean type private boolean isHot;public boolean isHot () {return isHot;} 3. Wrapper type private Boolean isHot;public Boolean getHot () {return isHot;} 4. Do not start with is private boolean hot;public boolean isHot () {return hot;} 5. Packaging type private Boolean hot;public Boolean getHot () {return hot;}
In fact, it is stated in the java development manual issued by Alibaba that it is mandatory that Boolean data, whether boolean or Boolean, are not allowed to be named by isXXX.
For parameters that are not of boolean type, the specification for naming getter and setter methods begins with get and set
For parameters of type boolean, the setter method begins with set, but the specification for naming the getter method begins with is
The names of the getter and setter methods automatically generated by the wrapper class are getXXX () and setXXX ()
In fact, there are corresponding provisions for these in the javaBeans specification. The getter and setter methods of the attributes of the basic data types are getXXX () and setXXX, but for the Boolean data in the basic data, there is another set of provisions, and the getter and setter methods are isXXX () and setXXX. But the packaging types all start with get.
This approach works fine at some times, but in some rpc frameworks, when the isSuccess () method is read by reverse parsing, the rpc framework "thinks" that its corresponding property value is success, but in fact its corresponding property value is isSuccess, resulting in the property value not being obtained, thus throwing an exception.
Summary:
1. It is not recommended to set the property value of boolean type to start with is, otherwise it will cause serialization exception of rpc framework.
2. If the isSuccess () method automatically generated by IDE is forcibly modified to getSuccess (), the Success attribute value can also be obtained. If the two coexist, then the Success attribute value can be obtained by the getSuccess () method.
Is it better to use basic types of data or packaging classes in your work?
Let us take an example, a profit calculation system, its profit ratio is positive and negative, if the basic type double is used to define the data, when RPC is called, if there is a problem, it should return an error, but due to the use of the basic type, returned 0.0, the system will think that there is no problem, break even this year, but will not find that there is an error. If the wrapper data type Double is used, null is returned when the RPC call fails, so you can see the problem directly without affecting the judgment because of the default value.
In fact, this is also mandatory in the Ali java development manual:
Therefore, it is recommended that you use wrapper data types in POJO and basic data types for local variables.
Thank you for your reading, the above is the content of "Why not recommend boolean type variables to use isXXX", after the study of this article, I believe you do not recommend boolean type variables to use isXXX this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.