What is an annotation such as @Override or @Service in Java?
Options
- A label (metadata) on code that the compiler or a framework reads (Correct option)
- A comment that the compiler and frameworks always ignore
- A mandatory import statement placed at the top of the file
- A kind of loop that repeats over a list
Answer
A label (metadata) attached to code that the compiler or a framework reads to change how it behaves
Why
Annotations start with @ and sit above classes, methods or fields.
Example: @Override tells the compiler to check that the method really overrides a parent method. @Service tells Spring to create and manage an object of that class.
Spring uses annotations everywhere, so reading them is a core skill: @RestController, @GetMapping, @Autowired, @Transactional.
Remember
Annotation = a label that frameworks read.