☕ Java Advanced

What are Java annotations and how do they work?

Why Interviewers Ask This

Advanced questions like this reveal whether a candidate has internalized Java deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.

Answer

Annotations are metadata attached to classes, methods, fields, or parameters that provide information to the compiler, tools, or runtime. They are declared with @interface. Built-in annotations: @Override (compiler check), @Deprecated, @SuppressWarnings, @FunctionalInterface. Meta-annotations configure the annotation itself: @Retention (SOURCE — discarded after compilation, CLASS — in bytecode but not at runtime, RUNTIME — available via reflection), @Target (where it can be applied), @Inherited, @Repeatable. Annotation processors (APT) run at compile time to generate code — this powers Lombok, Dagger, and Room. Runtime annotations are read via Reflection — this powers Spring's @Autowired, @RequestMapping, and JUnit's @Test.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.