Return to site

☕🗳 Do you use JetBrain annotations in your Java code? @NotNull/ @Nullable/ @Unmodifiable

· intellij,java
broken image

org.jetbrains.annotations.NotNull

org.jetbrains.annotations.Nullable

org.jetbrains.annotations.Unmodifiable

👩‍🏫 JetBrains annotations like @NotNull, @Nullable, and @Unmodifiable are used in Java and other programming languages to improve code clarity and safety. Here’s a brief overview of each:

1️⃣ @NotNull: This annotation indicates that a method should not return null, and variables (fields, local variables, and parameters) cannot hold a null value. It’s an explicit contract that helps prevent NullPointerExceptions by signaling to the developer and the IDE that null values are not acceptable here.

2️⃣ @Nullable: In contrast, this annotation signifies that a method could return null, and variables may be null. It’s useful for static analysis tools to identify potential sections of code where null checks are necessary to avoid runtime errors.

3️⃣ @Unmodifiable: This annotation is used to indicate that a collection is unmodifiable. When you annotate a collection with @Unmodifiable, it means that the contents of this collection are not meant to be changed. Attempting to modify such a collection (like adding or removing elements) should result in an UnsupportedOperationException.

☝ These annotations serve as a form of documentation that can also be enforced at compile-time or runtime, helping to catch errors early in the development process and making the codebase more robust and maintainable.

#java #programming #poll #jetbrain #annotations #notnull #nullable #unmodifiable

🔍Details: https://www.jetbrains.com/help/idea/annotating-source-code.html