10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8.4.4 Generic Methods CLASSES<br />

220<br />

defines a class which is designed for concurrent use. Each instance of the class<br />

Box has an instance variable boxContents that can hold a reference to any object.<br />

You can put an object in a Box by invoking put, which returns false if the box is<br />

already full. You can get something out of a Box by invoking get, which returns a<br />

null reference if the box is empty.<br />

If put and get were not synchronized, and two threads were executing<br />

methods for the same instance of Box at the same time, then the code could misbehave.<br />

It might, for example, lose track of an object because two invocations to put<br />

occurred at the same time.<br />

See §17 for more discussion of threads and locks.<br />

8.4.4 Generic Methods<br />

A method is generic if it declares one or more type variables (§4.4). <strong>The</strong>se type<br />

variables are known as the formal type parameters of the method. <strong>The</strong> form of the<br />

formal type parameter list is identical to a type parameter list of a class or interface,<br />

as described in §8.1.2.<br />

<strong>The</strong> scope of a method’s type parameter is the entire declaration of the<br />

method, including the type parameter section itself. <strong>The</strong>refore, type parameters<br />

can appear as parts of their own bounds, or as bounds of other type parameters<br />

declared in the same section.<br />

Type parameters of generic methods need not be provided explicitly when a<br />

generic method is invoked. Instead, they are almost always inferred as specified in<br />

§15.12.2.7<br />

8.4.5 Method Return Type<br />

DRAFT<br />

<strong>The</strong> return type of a method declares the type of value a method returns, if it<br />

returns a value, or states that the method is void.<br />

A method declaration d1 with return type R1 is return-type-substitutable for<br />

another method d2 with return type R2, if and only if the following conditions<br />

hold:<br />

• If R1 is a primitive type, then R2 is identical to R1.<br />

• If R1 is a reference type then:<br />

◆ R1 is either a subtype of R2 or R1 can be converted to a subtype of R2 by<br />

unchecked conversion (§5.1.9), or<br />

◆ R1 = | R2 |.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!