Skip to content
Advertisement

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

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

@TypeAnno Integer[] vec;

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

Advertisement

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).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement