Java Reference
In-Depth Information
A generic method declaration defines a set of methods, one for each possible invocation of
the type parameter section by type arguments. Type arguments may not need to be provided
explicitly when a generic method is invoked, as they can often be inferred (§ 15.12.2.7 ) .
The scope and shadowing of a method's type parameter is specified in § 6.3 .
8.4.5. Method Return Type
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:
Type
void
Return types may vary among methods that override each other if the return types are ref-
erence 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 , if and only if the following conditions hold:
• If R 1 is void then R 2 is void .
• If R 1 is a primitive type, then R 2 is identical to R 1 .
• If R 1 is a reference type then:
R 1 is either a subtype of R 2 or R 1 can be converted to a subtype of R 2 by un-
checked conversion (§ 5.1.9 ) , or
R 1 = | R 2 |
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.
8.4.6. Method Throws
A throws clause is used to declare any checked exception classes (§ 11.1.1 ) that the state-
ments in a method or constructor body can throw (§ 11.2.2 ) .
Search WWH ::




Custom Search