background

Boolean is a basic data type, while Boolean is a wrapper class. Why not use isXXX as the name? Is it better to use primitive types of data or wrapper classes? Java Interview guide PDF complete version

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. Private Boolean isHot; public Boolean getHot() { return isHot; } 4. Private Boolean hot; public boolean isHot() { return hot; } 5. Private Boolean hot; public Boolean getHot() { return hot; }Copy the code

In fact, the Java development manual issued by Alibaba states that it is mandatory for Boolean data, whether Boolean or Boolean, to be named isXXX

Add a description

  • For parameters of non-boolean types, the convention for naming getter and setter methods begins with GET and set

  • For Boolean parameters, setter methods begin with set, but the specification for getter method names begins with IS

  • The name of the getter and setter methods automatically generated by the wrapper class are getXXX() and setXXX()

  1. GetXXX () and setXXX are getXXX() and setXXX are getXXX and setXXX are getXXX() and setXXX are getXXX and setXXX are getXXX and setXXX are getXXX and setXXX are getXXX and setXXX are getXXX and setXXX are getXXX and setXXX are getXXX and setXXX are getXXX and setXXX are getXXX and setXXX are getXXX and setXXX are getXXX. Its getters and setters are isXXX() and setXXX. But the wrapper types all start with GET

  2. This method can work normally in some cases, but in some RPC frameworks, when the isSuccess() method is read by reverse parsing, the RPC framework will “think” that its corresponding attribute value isSuccess, but in fact its corresponding attribute value is isSuccess, resulting in the attribute value cannot be obtained. Throw an exception.

1. It is not recommended to set a Boolean attribute value starting with IS. Otherwise, the RPC framework will serialize an exception.

2, if the IDE automatically generated isSuccess() method is changed to getSuccess(), also can obtain the Success attribute value, if both exist, then through getSuccess() method to obtain the Success attribute value.

Is it better to work with basic types of data or wrapped classes?

Let’s take, for example, a calculation system of profit, the profit ratio is negative, if use the basic types of bouble defines data, when RPC calls, if there is a problem, should return an error, but due to the use of the basic types, returned 0.0, system will think no problem, this year the balance of payments, And you don’t know that there was a mistake. When the wrapper data type Double is used, null is returned when the RPC call fails, so that you can see the problem directly and not be influenced by the default value.

In fact, Ali Java development manual has a mandatory provision for this:

Add a description

Therefore, it is recommended that you use wrapper data types in POJOs and primitive data types for local variables. Java Interview guide PDF complete version