Use caution with Boolean types in Java beans

Source: Internet
Author: User

JavaBean is a standard, followed by a standard Bean is a Java class with attributes and Getters/setters methods.

The definition of JavaBean is simple, but there are a few places to be aware of, such as a bean with a Boolean type of attribute. We know that for a property, if it is named Test, its getter and setter methods are generally gettest () and settest. However, if test is a Boolean type, then its getter and setter methods are Istest () and Settest (). This is a difference

public class BeanTest {    private boolean test; public boolean isTest() { return test; } public void setTest(boolean test) { this.test = test; }}

If we change this property name to Istest, then its generated getter and setter method is actually the same as the property test.

public class BeanTest1 {    private boolean isTest; public boolean isTest() { return isTest; } public void setTest(boolean test) { isTest = test; }}

This difference is not affected in the general case, but it is important to note that if the conversion is involved with the JSON string. For example, if I json the objects of the two beans above, the result is actually the same

public static void main(String[] args) {    System.out.println(JSON.toJSONString(new Bean1())); //{"test":false} System.out.println(JSON.toJSONString(new Bean2())); //{"test":false}}

If, I want to generate {"Istest": false} such a JSON string, then our bean how to define it? At this point we should not rely on idea to automatically generate for us, we must manually write:

public class Bean3{    private boolean isTest; public boolean getIsTest(){ return isTest; } public void setIsTest(boolean isTest){ this.isTest = isTest; }}

While this generated the JSON string we wanted, it didn't follow the Java specification and it felt awkward .... We can use @jsonfield to specify the corresponding field names after the JSON.

Also, if the attribute is a Boolean wrapper type Boolean, then what is the getter and setter method defined by JavaBean?

public class Bean4{ Span class= "Hljs-keyword" >private Boolean test; public Boolean gettest () {return Test } public void setTest (Boolean test) {this.test = Test;}} public class bean5{private Boolean istest; public Boolean gettest () {return Istest; } public void setTest (Boolean test) {istest = test;}}               

We have found that the get and set methods of Boolean and Boolean properties are also different.

In general, in order to avoid trouble, whether it is to define a Boolean-type property, or to define a Boolean-type property, its field names do not use isxxx this way, and then follow the bean specification to generate the get and set method is good

Use caution with Boolean types in Java beans

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.