The result of a method declaration either declares the type of value that the method returns (the return type), or uses the keyword void to indicate that the method does not return a value.
Result:
UnannType
void
If the result is not void, then the return type of a method is denoted by UnannType if no bracket pairs appear after the formal parameter list, and is specified by §10.2 otherwise.
Return types may vary among methods that override each other if the return types are reference types. The notion of return-type-substitutability supports covariant returns, that is, the specialization of the return type to a subtype.
A method declaration d_1 with return type R_1 is return-type-substitutable for another method d_2 with return type R_2 iff any of the following is true:
-
If R_1 is
voidthen R_2 isvoid. -
If R_1 is a primitive type then R_2 is identical to R_1.
-
If R_1 is a reference type then one of the following is true:
An unchecked conversion is allowed in the definition, despite being unsound, as a special allowance to allow smooth migration from non-generic to generic code. If an unchecked conversion is used to determine that R_1 is return-type-substitutable for R_2, then R_1 is necessarily not a subtype of R_2 and the rules for overriding (§8.4.8.3, §9.4.1) will require a compile-time unchecked warning.