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
  1. Packing type
private Boolean isHot;
    public Boolean getHot() {
        return isHot;
    }

Copy the code
  1. Do not start with is
private boolean hot;
    public boolean isHot() {
        return hot;
    }

Copy the code
  1. Packing type
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 that Boolean data, whether Boolean or Boolean, cannot be called using 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 names of the getter and setter methods automatically generated by the wrapper classgetXXX()andsetXXX()
  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 can work in some cases, but in some RPC frameworks, when the isSuccess() method is read by a reverse lookup, the RPC framework will “assume” that the corresponding property value isSuccess, when in fact the corresponding property value is isSuccess. The attribute value cannot be obtained and an exception is thrown.

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/belongtocod…


Recommend 3 original Springboot +Vue projects, with complete video explanation and documentation and source code:

Build a complete project from Springboot+ ElasticSearch + Canal

  • Video tutorial: www.bilibili.com/video/BV1Jq…
  • A complete development documents: www.zhuawaba.com/post/124
  • Online demos: www.zhuawaba.com/dailyhub

【VueAdmin】 hand to hand teach you to develop SpringBoot+Jwt+Vue back-end separation management system

  • Full 800 – minute video tutorial: www.bilibili.com/video/BV1af…
  • Complete development document front end: www.zhuawaba.com/post/18
  • Full development documentation backend: www.zhuawaba.com/post/19

【VueBlog】 Based on SpringBoot+Vue development of the front and back end separation blog project complete teaching

  • Full 200 – minute video tutorial: www.bilibili.com/video/BV1af…
  • Full development documentation: www.zhuawaba.com/post/17

If you have any questions, please come to my official account [Java Q&A Society] and ask me