Types are used in most kinds of declaration and in certain kinds of expression. Specifically, there are 16 type contexts where types are used:
-
In declarations:
- A type in the
extendsorimplementsclause of a class declaration (§8.1.4, §8.1.5, §8.5, §9.5) - A type in the
extendsclause of an interface declaration (§9.1.3, §8.5, §9.5) - The return type of a method (including the type of an element of an annotation type) (§8.4.5, §9.4, §9.6.1)
- A type in the
throwsclause of a method or constructor (§8.4.6, §8.8.5, §9.4) - A type in the
extendsclause of a type parameter declaration of a generic class, interface, method, or constructor (§8.1.2, §9.1.2, §8.4.4, §8.8.4) - The type in a field declaration of a class or interface (including an enum constant) (§8.3, §9.3, §8.9.1)
- The type in a formal parameter declaration of a method, constructor, or lambda expression (§8.4.1, §8.8.1, §9.4, §15.27.1)
- The type of the receiver parameter of a method (§8.4.1)
- The type in a local variable declaration (§14.4, §14.14.1, §14.14.2, §14.20.3)
- The type in an exception parameter declaration (§14.20)
- A type in the
-
In expressions:
- A type in the explicit type argument list to an explicit constructor invocation statement or class instance creation expression or method invocation expression (§8.8.7.1, §15.9, §15.12)
- In an unqualified class instance creation expression, as the class type to be instantiated (§15.9) or as the direct superclass or direct superinterface of an anonymous class to be instantiated (§15.9.5)
- The element type in an array creation expression (§15.10.1)
- The type in the cast operator of a cast expression (§15.16)
- The type that follows the
instanceofrelational operator (§15.20.2) - In a method reference expression (§15.13), as the reference type to search for a member method or as the class type or array type to construct.
Also, types are used as:
- The element type of an array type in any of the above contexts; and
- A non-wildcard type argument, or a bound of a wildcard type argument, of a parameterized type in any of the above contexts.
Finally, there are three special terms in the Java programming language which denote the use of a type:
- An unbounded wildcard (§4.5.1)
- The
...in the type of a variable arity parameter (§8.4.1), to indicate an array type - The simple name of a type in a constructor declaration (§8.8), to indicate the class of the constructed object
The meaning of types in type contexts is given by:
- §4.2, for primitive types
- §4.4, for type parameters
- §4.5, for class and interface types that are parameterized, or appear either as type arguments in a parameterized type or as bounds of wildcard type arguments in a parameterized type
- §4.8, for class and interface types that are raw
- §4.9, for intersection types in the bounds of type parameters
- §6.5, for class and interface types in contexts where genericity is unimportant (§6.1)
- §10.1, for array types
Some type contexts restrict how a reference type may be parameterized:
-
The following type contexts require that if a type is a parameterized reference type, it has no wildcard type arguments:
- In an
extendsorimplementsclause of a class declaration (§8.1.4, §8.1.5) - In an
extendsclause of an interface declaration (§9.1.3) - In an unqualified class instance creation expression, as the class type to be instantiated (§15.9) or as the direct superclass or direct superinterface of an anonymous class to be instantiated (§15.9.5)
- In a method reference expression (§15.13), as the reference type to search for a member method or as the class type or array type to construct.
In addition, no wildcard type arguments are permitted in the explicit type argument list to an explicit constructor invocation statement or class instance creation expression or method invocation expression or method reference expression (§8.8.7.1, §15.9, §15.12, §15.13).
- In an
-
The following type contexts require that if a type is a parameterized reference type, it has only unbounded wildcard type arguments (i.e. it is a reifiable type) :
-
The following type contexts disallow a parameterized reference type altogether, because they involve exceptions and the type of an exception is non-generic (§6.1):
In any type context where a type is used, it is possible to annotate the keyword denoting a primitive type or the Identifier denoting the simple name of a reference type. It is also possible to annotate an array type by writing an annotation to the left of the [ at the desired level of nesting in the array type. Annotations in these locations are called type annotations, and are specified in §9.7.4. Here are some examples:
@Foo int[] f;annotates the primitive typeintint @Foo [] f;annotates the array typeint[]int @Foo [][] f;annotates the array typeint[][]int[] @Foo [] f;annotates the array typeint[]which is the component type of the array typeint[][]
Five of the type contexts which appear in declarations occupy the same syntactic real estate as a number of declaration contexts (§9.6.4.1):
- The return type of a method (including the type of an element of an annotation type)
- The type in a field declaration of a class or interface (including an enum constant)
- The type in a formal parameter declaration of a method, constructor, or lambda expression
- The type in a local variable declaration
- The type in an exception parameter declaration
The fact that the same syntactic location in a program can be both a type context and a declaration context arises because the modifiers for a declaration immediately precede the type of the declared entity. §9.7.4 explains how an annotation in such a location is deemed to appear in a type context or a declaration context or both.
Example 4.11-1. Usage of a Type
import java.util.Random;
import java.util.Collection;
import java.util.ArrayList;
class MiscMath<T extends Number> {
int divisor;
MiscMath(int divisor) { this.divisor = divisor; }
float ratio(long l) {
try {
l /= divisor;
} catch (Exception e) {
if (e instanceof ArithmeticException)
l = Long.MAX_VALUE;
else
l = 0;
}
return (float)l;
}
double gausser() {
Random r = new Random();
double[] val = new double[2];
val[0] = r.nextGaussian();
val[1] = r.nextGaussian();
return (val[0] + val[1]) / 2;
}
Collection<Number> fromArray(Number[] na) {
Collection<Number> cn = new ArrayList<Number>();
for (Number n : na) cn.add(n);
return cn;
}
<S> void loop(S s) { this.<S>loop(s); }
}
In this example, types are used in declarations of the following:
- Imported types (§7.5); here the type
Random, imported from the typejava.util.Randomof the packagejava.util, is declared - Fields, which are the class variables and instance variables of classes (§8.3), and constants of interfaces (§9.3); here the field
divisorin the classMiscMathis declared to be of typeint - Method parameters (§8.4.1); here the parameter
lof the methodratiois declared to be of typelong - Method results (§8.4); here the result of the method
ratiois declared to be of typefloat, and the result of the methodgausseris declared to be of typedouble - Constructor parameters (§8.8.1); here the parameter of the constructor for
MiscMathis declared to be of typeint - Local variables (§14.4, §14.14); the local variables
randvalof the methodgausserare declared to be of typesRandomanddouble[](array ofdouble) - Exception parameters (§14.20); here the exception parameter
eof thecatchclause is declared to be of typeException - Type parameters (§4.4); here the type parameter of
MiscMathis a type variableTwith the typeNumberas its declared bound - In any declaration that uses a parameterized type; here the type
Numberis used as a type argument (§4.5.1) in the parameterized typeCollection<Number>.
and in expressions of the following kinds:
- Class instance creations (§15.9); here a local variable
rof methodgausseris initialized by a class instance creation expression that uses the typeRandom - Generic class (§8.1.2) instance creations (§15.9); here
Numberis used as a type argument in the expressionnew ArrayList<Number>() - Array creations (§15.10.1); here the local variable
valof methodgausseris initialized by an array creation expression that creates an array ofdoublewith size 2 - Generic method (§8.4.4) or constructor (§8.8.4) invocations (§15.12); here the method
loopcalls itself with an explicit type argumentS - Casts (§15.16); here the
returnstatement of the methodratiouses thefloattype in a cast - The
instanceofoperator (§15.20.2); here theinstanceofoperator tests whethereis assignment-compatible with the typeArithmeticException