Why Using isXXX for Boolean Fields Is a Bad Idea in Java

This article explains the Java naming conventions for boolean and Boolean fields, why using the isXXX prefix can cause serialization issues in RPC frameworks, and recommends using wrapper types for POJO properties while keeping primitives for local variables.

Programmer DD
Programmer DD
Programmer DD
Why Using isXXX for Boolean Fields Is a Bad Idea in Java

Background: In everyday development we often use boolean primitive types and the Boolean wrapper class, but naming boolean getters with the isXXX pattern is discouraged.

1. Other non‑boolean type
   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. Not starting with is
   private boolean hot;
   public boolean isHot() {
       return hot;
   }
5. Wrapper type without is
   private Boolean hot;
   public Boolean getHot() {
       return hot;
   }

Alibaba Java Development Manual: The manual explicitly forbids using isXXX for both primitive boolean and wrapper Boolean fields.

For non‑boolean parameters, getters and setters must start with get and set.

For boolean parameters, the setter starts with set but the getter should start with is.

Wrapper‑generated getters and setters always use getXXX() and setXXX().

The JavaBeans specification defines that primitive boolean properties use isXXX() for getters, while wrapper Boolean properties use getXXX().

Some RPC frameworks interpret an isSuccess() method as referring to a property named success. If the actual field is named isSuccess, the framework cannot map it correctly, leading to serialization errors.

Summary: Do not name boolean property getters with the is prefix for fields that may be serialized, as it can cause RPC framework exceptions.

If you must rename an automatically generated isSuccess() method, change it to getSuccess() so the property can be accessed correctly; having both methods allows retrieval via getSuccess().

Choosing Between Primitive and Wrapper Types

Consider a profit‑calculation system where the profit ratio can be positive or negative. Using a primitive double means a failed RPC call returns 0.0, which may be mistaken for a valid result. Using the wrapper Double returns null on failure, making the error obvious.

Recommendation from the Alibaba Java Manual: Use wrapper types for POJO fields and primitive types for local variables.

Note

The Alibaba Java Development Manual goes beyond the language itself, defining essential qualities for competent developers and serving as a reference for engineers, educators, and job seekers.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentRPCbooleannaming conventionsWrapper TypesAlibaba Java Manual
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.