Lesson 37 / 38
What Are Annotations?
Understand what annotations are and are not, and get comfortable with the handful of built-in ones you'll see in almost every Java codebase.
Metadata, not behaviour
An annotation (@Something) attaches metadata to code — a class, method, field, or parameter — that the compiler, a tool, or a framework (like Spring) can read and act on. The annotation itself does nothing at runtime unless something else is written to look for it.
The essential built-ins
@Override asks the compiler to verify a method really does override a supertype method — catches typos immediately. @Deprecated marks an API as discouraged, producing a compiler warning at call sites. @FunctionalInterface asks the compiler to verify an interface has exactly one abstract method, so it can be used as a lambda target.
class Old {
@Deprecated
void legacyMethod() { /* callers now get a compiler warning */ }
}
class Dog extends Animal {
@Override
String sound() { return "woof"; } // typo in the name -> compile error, not a silent new method
}
@FunctionalInterface
interface Transformer<T, R> {
R apply(T input); // exactly one abstract method
}Where you'll see more
Frameworks lean heavily on custom annotations — @Test (JUnit), @Entity (JPA), @RestController (Spring). Those live in Advanced Java; for Core Java, knowing what an annotation is and reading the built-ins comfortably is enough.