What are Java annotations and how do they work?
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.
Previous
What is the difference between Callable and Runnable in Java?
Next
What is Virtual Threads in Java (Project Loom)?