Quick revision
The short answer
Type annotations, added in Java 8, are annotations declared with @Target(ElementType.TYPE_USE), which can be written on any use of a type rather than only on a declaration: List<@NonNull String>, a cast to (@NonNull Object), new @Interned String("a"), or String @NonNull [] for an array. They do nothing at run time by themselves. Pluggable type checkers such as the Checker Framework read them during compilation to add properties like nullness, immutability or tainting to the type system and report errors.
What to remember
In this answer, we saw that type annotations, declared with @Target(TYPE_USE), can appear on any use of a type: generic arguments, casts, object creation, arrays, supertypes, thrown types, nested types and the receiver parameter. Pluggable checkers such as the Checker Framework read them at compile time to add properties like nullness, interning or tainting to the type system, and RUNTIME ones can be read through AnnotatedType. By themselves they change nothing at run time, and for arrays the annotation describes whatever type follows it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Type annotations, added in Java 8, are annotations declared with @Target(ElementType.TYPE_USE), which can be written on any use of a type rather than only on a declaration: List<@NonNull String>, a cast to (@NonNull Object), new @Interned String("a"), or String @NonNull [] for an array. They do nothing at run time by themselves. Pluggable type checkers such as the Checker Framework read them during compilation to add properties like nullness, immutability or tainting to the type system and report errors.
Why this question is asked
Before Java 8 we could annotate a field, a parameter or a method, but not the type inside a generic, a cast or an array. That made annotations too coarse to describe something as basic as a list whose elements are never null. Type annotations, from JSR 308, fixed that, and today they are the foundation of the nullness annotations we see in JSpecify, Spring and Kotlin interop. Interviewers ask about them because the syntax has a few traps and because they reveal whether we understand that annotations are only metadata. In this answer, we will recap declaration annotations, see where type annotations can appear, look at how a checker uses them, read them with reflection, and settle the array syntax question.
1A Quick Recap: Declaration Annotations Versus Type Annotations
A declaration annotation attaches to something we declare: a class, a method, a field, a parameter. @Override and @Deprecated are examples. Each one describes the declaration as a whole.
A type annotation attaches to a type wherever that type is used. The difference matters as soon as types are nested. In List<String> names there is one declaration, the field names, but two types, List and String. A declaration annotation can only say something about the field. A type annotation can say something about the String inside the List. We declare one by putting TYPE_USE in its target.
There is also ElementType.TYPE_PARAMETER, which allows an annotation on a type variable declaration such as class Box<@NonNull T>. TYPE_USE covers every other use.
2Where Type Annotations Can Appear
The rule is simple to state: wherever we write a type, we can put a type annotation in front of it. The examples below each compile with the NonNull and Interned annotations above.
| Position | Example | What it annotates |
|---|---|---|
| Generic argument | List<@NonNull String> | The elements of the list |
| Cast | (@NonNull String) x | The result of the cast |
| Object creation | new @Interned String("a") | The type of the new object |
| Array | String @NonNull [] | The array reference itself |
| Supertype or throws | implements @Readonly Comparable<T> | The inherited or thrown type |
Two of these surprise people. For a nested type the annotation goes right before the simple name, Outer.@NonNull Inner, because @NonNull Outer.Inner would annotate Outer, which is not a type being used here. And the receiver parameter, an optional first parameter named this, exists only so that we have a place to annotate the type of the object a method is called on. It changes nothing in the compiled code.
3How the Checker Framework Uses Them
Java's compiler only checks the types the language knows about. A pluggable type checker extends that check with a new property. The Checker Framework is the best-known one: it runs as an annotation processor inside javac, reads the type annotations, and verifies that every assignment, call and dereference respects them. If the code could put a null where @NonNull is required, compilation fails, just as it would for assigning a String to an int.
Notice that the checker follows the flow of the code: after the null check it knows name is non-null on that branch. The same framework has checkers for other properties, for example @Interned strings that can be compared with ==, immutability, units of measurement, and tainting, where @Tainted data from a user must pass through a sanitiser before reaching a SQL query. Each property is a new layer of types on top of Java's own.
4Reading Type Annotations With Reflection
Type annotations with RUNTIME retention are stored in the class file and can be read through the AnnotatedType API, added in Java 8. Each reflective element that has a type, such as a field, a parameter or a method's return, offers an annotated view of that type that we can walk into.
So a framework could, for example, validate at run time that a list has no null elements. But there is a catch that people often miss: the annotation by itself changes nothing. Adding @NonNull to List<String> does not stop anybody from adding null to the list. Only a checker at compile time, or a framework that reads the annotation and acts on it, gives the annotation any effect.
5Follow-up questions
What is the difference between @NonNull String[] and String @NonNull [] ?
For a type annotation, the position decides which type is annotated. In @NonNull String[] the annotation sits before String, so it describes the elements: an array of non-null strings, while the array reference itself may be null. In String @NonNull [] it sits before the brackets, so it describes the array: the array reference is non-null, but its elements may be null. String @NonNull [] @Nullable [] reads left to right as a non-null array of possibly-null arrays. One caution: if the annotation also allows FIELD or PARAMETER targets, @NonNull String[] on a field annotates both the declaration and the element type.
Do type annotations change the bytecode that runs?
No. With CLASS or RUNTIME retention they are stored as attributes in the class file, but the instructions the JVM executes are exactly the same as without them. Any behaviour comes from a tool or framework that reads them.
Can an annotation be both a declaration and a type annotation?
Yes, by listing several targets, such as FIELD, PARAMETER and TYPE_USE. Older nullness annotations often did this, which made positions ambiguous. Newer designs such as JSpecify use TYPE_USE only, so each annotation has exactly one meaning.
6Conclusion
In this answer, we saw that type annotations, declared with @Target(TYPE_USE), can appear on any use of a type: generic arguments, casts, object creation, arrays, supertypes, thrown types, nested types and the receiver parameter. Pluggable checkers such as the Checker Framework read them at compile time to add properties like nullness, interning or tainting to the type system, and RUNTIME ones can be read through AnnotatedType. By themselves they change nothing at run time, and for arrays the annotation describes whatever type follows it.
Open with
Type annotations came with Java 8 through JSR 308. An annotation whose target includes TYPE_USE can be written on any use of a type, not only on declarations.
Then, if they want more
So I can write List of at-NonNull String to say the elements are non-null, put an annotation in a cast like (@NonNull String) x, in object creation like new @Interned String, on supertypes and thrown types, and on arrays.
For arrays, the annotation applies to the type right after it: @NonNull String[] means the elements are non-null, while String @NonNull [] means the array reference itself is non-null. On their own they do nothing at run time; the bytecode is the same. Their value comes from pluggable type checkers.
The Checker Framework runs as an annotation processor in javac and treats the annotations as extra types: for nullness, it reports an error if I dereference a @Nullable value without checking it, and it understands flow, so a null check makes the value non-null in that branch. There are also checkers for interning, immutability and tainted input.
If the annotations have RUNTIME retention, frameworks can read them through the AnnotatedType API, for example getAnnotatedActualTypeArguments on a field's generic type. Today JSpecify's nullness annotations are TYPE_USE annotations built on this.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What are type annotations introduced in Java 8? Give examples of where they can appear...": https://scrivio-interview-library.netlify.app/java/annotations/a83cafd989/