Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java_language_specification_7.pdf
Скачиваний:
28
Добавлен:
21.03.2016
Размер:
3.11 Mб
Скачать

4.3 Reference Types and Values TYPES, VALUES, AND VARIABLES

4.3 Reference Types and Values

There are four kinds of reference types: class types (§8), interface types (§9), type variables (§4.4), and array types (§10).

ReferenceType:

ClassOrInterfaceType

TypeVariable

ArrayType

ClassOrInterfaceType:

ClassType

InterfaceType

ClassType:

TypeDeclSpecifier TypeArgumentsopt

InterfaceType:

TypeDeclSpecifier TypeArgumentsopt

TypeDeclSpecifier:

Identifier

ClassOrInterfaceType . Identifier

TypeName:

Identifier

TypeName . Identifier

TypeVariable:

Identifier

ArrayType:

Type [ ]

The sample code:

class Point { int[] metrics; }

interface Move { void move(int deltax, int deltay); }

declares a class type Point, an interface type Move, and uses an array type int[] (an array of int) to declare the field metrics of the class Point.

52

TYPES, VALUES, AND VARIABLES

Reference Types and Values

4.3

A class or interface type consists of a type declaration specifier, optionally followed by type arguments (§4.5.1). If type arguments appear anywhere in a class or interface type, it is a parameterized type (§4.5).

A type declaration specifier may be either a type name (§6.5.5), or a class or interface type followed by "." and an identifier. In the latter case, the specifier has the form T.id, where id must be the simple name of an accessible (§6.6) member type (§8.5, §9.5) of T, or a compile-time error occurs. The specifier denotes that member type.

There are contexts in the Java programming language where a generic class or interface name is used without providing type arguments. Such contexts do not involve the use of raw types (§4.8). Rather, they are contexts where type arguments are unnecessary for, or irrelevant to, the meaning of the generic class or interface.

For example, a single-type-import declaration import java.util.List; puts the simple type name List in scope within a compilation unit so that parameterized types of the form List<...> may be used. As another example, invocation of a static method of a generic class needs only to give the (possibly qualified) name of the generic class without any type arguments, because such type arguments are irrelevant to a static method. (The method itself may be generic, and take its own type arguments, but the type parameters of a static method are necessarily unrelated to the type parameters of its enclosing generic class (§6.5.5).)

Because of the occasional need to use a generic class or interface name without type arguments, type names are distinct from type declaration specifiers. A type name is always qualified by means of another type name. In some cases, this is necessary to access an inner class that is a member of a parameterized type.

Here is an example of where a type declaration specifier is distinct from a type name:

class GenericOuter<T extends Number> {

public class Inner<S extends Comparable<S>> { T getT() { return null;}

S getS() { return null;}

}

}

class Test {

public static void main(String[] args) { GenericOuter<Integer>.Inner<Double> x1 = null; Integer i = x1.getT();

Double d = x1.getS();

}

}

If we accessed Inner by qualifying it with a type name, as in:

GenericOuter.Inner x2 = null;

53

4.3.1

Objects

TYPES, VALUES, AND VARIABLES

we would force its use as a raw type, losing type information.

4.3.1 Objects

An object is a class instance or an array.

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

A class instance is explicitly created by a class instance creation expression (§15.9).

An array is explicitly created by an array creation expression (§15.10).

A new class instance is implicitly created when the string concatenation operator + (§15.18.1) is used in a non-constant (§15.28) expression, resulting in a new object of type String (§4.3.3).

A new array object is implicitly created when an array initializer expression (§10.6) is evaluated; this can occur when a class or interface is initialized (§12.4), when a new instance of a class is created (§15.9), or when a local variable declaration statement is executed (§14.4).

New objects of the types Boolean, Byte, Short, Character, Integer, Long, Float, and Double may be implicitly created by boxing conversion (§5.1.7).

Example 4.3.1-1. Object Creation

class Point { int x, y;

Point() { System.out.println("default"); } Point(int x, int y) { this.x = x; this.y = y; }

/* A Point instance is explicitly created at class initialization time: */

static Point origin = new Point(0,0);

/* A String can be implicitly created by a + operator: */

public String toString() { return "(" + x + "," + y + ")"; }

}

class Test {

public static void main(String[] args) { /* A Point is explicitly created

using newInstance: */ Point p = null;

try {

p = (Point)Class.forName("Point").newInstance(); } catch (Exception e) {

System.out.println(e);

54

TYPES, VALUES, AND VARIABLES

Objects

4.3.1

}

/* An array is implicitly created by an array constructor: */

Point a[] = { new Point(0,0), new Point(1,1) };

/* Strings are implicitly created by + operators: */

System.out.println("p: " + p);

System.out.println("a: { " + a[0] + ", " + a[1] + " }");

/* An

array

is

explicitly created

by

an array

creation expression: */

String sa[]

=

new String[2];

sa[0]

= "he";

sa[1] = "llo";

System.out.println(sa[0] + sa[1]);

}

}

This program produces the output:

default p: (0,0)

a: { (0,0), (1,1) } hello

The operators on references to objects are:

Field access, using either a qualified name (§6.6) or a field access expression (§15.11)

Method invocation (§15.12)

The cast operator (§5.5, §15.16)

The string concatenation operator + (§15.18.1), which, when given a String operand and a reference, will convert the reference to a String by invoking the toString method of the referenced object (using "null" if either the reference

or the result of toString is a null reference), and then will produce a newly created String that is the concatenation of the two strings

The instanceof operator (§15.20.2)

The reference equality operators == and != (§15.21.3)

The conditional operator ? : (§15.25).

There may be many references to the same object. Most objects have state, stored in the fields of objects that are instances of classes or in the variables that are the components of an array object. If two variables contain references to the same object, the state of the object can be modified using one variable's reference to the

55

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]