Why is the element type here annotated as Integer and not Integer[]? [closed]

Tags: ,



Let’s say that @TypeAnno is some type annotation.

@TypeAnno Integer[] vec;

Here the element type Integer is annotated and not Integer[]. Why?

Answer

A type annotation applies to the immediately following type component.

Integer[] is a compond type that consists of two parts: [] is the array part and Integer is the element type. It is desirable to be able to write a type annotation on each of the parts. @Nullable Object @NonNull [] is a non-null array of possibly-null objects.

For more information, See the Type Annotations FAQ entries What is the meaning of array annotations such as @NonNull Object @Nullable []? and I don’t like array and receiver annotations.. Also see section Syntax of array annotations in the type annotation specifiaction. This is also addressed in the Java Language Specification (for example, section Where Annotations May Appear).



Source: stackoverflow