ReviseAlgo Logo

Annotations

Built-in Annotations

Learn Java standard built-in annotations like @Override, @Deprecated, @SuppressWarnings, and @SafeVarargs.

Interview: Commonly tested on built-in compiler annotations, safe varargs rules, and suppressing specific warnings.

Last Updated: June 13, 2026 8 min read

Java defines standard built-in annotations that instruct the compiler to perform validation checks or suppress warning logs.

Core Idea

Standard annotations provide instruction rules directly to the javac compiler.

Why It Matters

Using annotations like @Override catches method signature typos at compile time instead of failing at runtime.

Interview Lens

Focuses on safe varargs rules and when @SuppressWarnings can hide dangerous bugs.

Core Built-in Annotations

  • @Override: Instructs the compiler to verify that the method overrides a method declared in a parent class or interface.
  • @Deprecated: Flags a code element as obsolete. The compiler generates warnings if other code accesses this element.
  • @SuppressWarnings("name"): Suppresses specific compiler warnings (e.g. "unchecked", "deprecation").
  • @SafeVarargs: Suppresses unchecked warnings when using generic varargs parameters. Can only be applied to final methods, static methods, or private constructors/methods (Java 9+).

Code Walkthrough

This method demonstrates generic varargs safely using the @SafeVarargs annotation.

import java.util.List;

public class BuiltInDemo { @SafeVarargs // Suppresses heap pollution warnings public static void printElements(List... lists) { for (List list : lists) { System.out.println(list); } } }

Interview-Relevant Information

Q: Why does generic varargs trigger compiler warnings?
Answer: In Java, varargs are compiled into an array. However, generic arrays cannot be created safely due to type erasure (heap pollution). Since the JVM cannot check generic types in arrays at runtime, referencing this array can cause ClassCastException. @SafeVarargs is used if you promise the method body only reads from the array safely without mutations.

Quick Checklist

What is heap pollution? When can you use @SafeVarargs? If yes, you understand standard annotations.

Use Cases

Cleaning compile log warnings inside legacy code bases.

Enforcing interface contract matches using Override.

Common Mistakes

Using broad @SuppressWarnings("all"), which hides severe type safety warnings and bugs.

Applying @SafeVarargs to a method that puts elements into the varargs array, causing heap pollution.