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?

example

1. Other non-Boolean types

 private String isHot;
    public String getIsHot() {
        return isHot;
    }
Copy the code

2. The Boolean type

 private boolean isHot;
    public boolean isHot() {
        return isHot;
    }
Copy the code

3. Type of packaging

 private Boolean isHot;
    public Boolean getHot() {
        return isHot;
    }
Copy the code

4. Do not start with is

  private boolean hot;
    public boolean isHot() {
        return hot;
    }
Copy the code

5. Type of packaging

 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

  • 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()

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, so that the attribute value cannot be obtained. Throw an exception.

conclusion

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:Therefore, it is recommended that you use wrapper data types in POJOs and primitive data types for local variables.

Source: blog.csdn.net/belongtocode/article/details/100635246