Understanding the Detailed Usage of @Nullable Annotation

This article explains how the @Nullable annotation can be applied to methods, fields, and parameters in Java, provides concrete code examples for each case, and shows a real‑world usage from Spring's StringUtils utility.

Coder Trainee
Coder Trainee
Coder Trainee
Understanding the Detailed Usage of @Nullable Annotation

You might think this simple, common annotation is something you already know.

The @Nullable annotation can be placed on methods, fields, and parameters. When used on a method, it indicates that the return value may be null; on a field, it signals that the field value may be null; on a parameter, it marks the argument as possibly null.

Example of using @Nullable on a method return value:

@Nullable
public ApiResult upload(@NotNull(message = "#x4E0A#x4F20#x53C2#x6570#x4E0D#x80FD#x4E3A#x7A7A;") @RequestParam("file") MultipartFile[] file) throws BaseException {
    ApiResult apiResult = new ApiResult();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    String format = simpleDateFormat.format(new Date());
    String realPath = filePath + File.separator + format;
    String returnPath = format;
    File targetFile = new File(realPath);
    if (!targetFile.exists()){
        targetFile.mkdirs();
    }
}

Annotating the method with @Nullable indicates that the method’s return value can be null.

Example of using @Nullable on a parameter:

private void checkUser(String fansid, String openid, @Nullable String op) throws BaseException{
    Consumer consumer = consumerService.selectByPrimaryKey(fansid);
    if (consumer == null) {
        throw new ParamException("#x7528#x6237#x4E0D#x5B58#x5728;");
    }
    Consumer consumer1 = consumerService.selectByPrimaryKey(openid);
    if(consumer1 == null){
        throw new ParamException("#x88AB#x5173#x6CE8#x8005#x4FE1#x606F#x5F02#x5E38;");
    }
}

Placing @Nullable before a parameter marks that argument as allowed to be null.

Example of using @Nullable on a field:

@Validated
@RestController
@RequestMapping("miniapi/follow")
public class FollowController extends BaseController {
    @Nullable
    private String isTime;

    @Autowired
    private FollowService followService;

    @Autowired
    private ConsumerService consumerService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
}

The @Nullable annotation above the isTime field indicates that this field may be null.

Beyond our own code, third‑party libraries also use @Nullable. In the Spring toolkit, the org.springframework.util.StringUtils class employs @Nullable in its empty‑check method.

Spring Toolkit Source Code Usage Example

Spring’s StringUtils.isEmpty method is defined as follows:

public static boolean isEmpty(@Nullable Object str) {
   return (str == null || "".equals(str));
}

That concludes our brief overview of @Nullable usage. Feel free to leave comments or discuss further.

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.

BackendJavaSpringannotation@Nullable
Coder Trainee
Written by

Coder Trainee

Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.

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.