Avatar ·

Which Java @NonNull/@NotNull annotation is best to use? Comparison and recommendations

Hello everyone! I'm a beginner Java developer, and for the second day I've been racking my brains over what seems like a simple question: which @NotNull (or @NonNull) annotation should I use in a project?

\n\n

The problem is that I see them in different places and don't understand which one is "correct". For example, IntelliJ IDEA has its own org.jetbrains.annotations.NotNull. In a Spring Boot project, I came across javax.annotation.Nonnull (now jakarta.annotation.Nonnull). And a colleague said it's better to use jakarta.validation.constraints.NotNull or lombok.NonNull.

\n\n

Here's a simple example that confuses me:

\n\n
import org.jetbrains.annotations.NotNull;\n\npublic class UserService {\n\n    public String g