CHAPTER 4: Types, Values, and Variables Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

4.3 Reference Types, Objects, and Reference Values

4.3.1 Objects , 4.3.2 The Class Object , 4.3.3 The Class String , 4.3.4 When Reference Types Are the Same

There are three kinds of reference types: class types (Chapter 8), interface types (Chapter 9), and array types (Chapter 10).


ReferenceType:

	ClassOrInterfaceType

	ArrayType

ClassOrInterfaceType:

	ClassType

	InterfaceType

ClassType:

	TypeName

InterfaceType:

	TypeName

ArrayType:

	Type [ ]

Names are described in Chapter 6; type names in S6.5 and, specifically, S6.5.4.

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 .


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 (S15.8), or by invoking the newInstance method of class Class (S20.3.8). An array is explicitly created by an array creation expression (S15.8).

A new class instance is implicitly created when the string concatenation operator + (S15.17.1) is used in an expression, resulting in a new object of type String (S4.3.3, S20.12). A new array object is implicitly created when an array initializer expression (S10.6) is evaluated; this can occur when a class or interface is initialized (S12.4), when a new instance of a class is created (S15.8), or when a local variable declaration statement is executed (S14.3).

Many of these cases are illustrated in the following example:


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);
		}


		// 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]);
	}
}

which produces the output:


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

The operators on references to objects are:

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 object, and then the altered state can be observed through the reference in the other variable.

The example program:

class Value { int val; }


class Test {
	public static void main(String[] args) {
		int i1 = 3;
		int i2 = i1;
		i2 = 4;
		System.out.print("i1==" + i1);
		System.out.println(" but i2==" + i2);
		Value v1 = new Value();
		v1.val = 5;
		Value v2 = v1;
		v2.val = 6;
		System.out.print("v1.val==" + v1.val);
		System.out.println(" and v2.val==" + v2.val);
	}
}

produces the output:


i1==3 but i2==4
v1.val==6 and v2.val==6

because v1.val and v2.val reference the same instance variable (S4.5.3) in the one Value object created by the only new expression, while i1 and i2 are different variables.

See Chapter 10 and S15.9 for examples of the creation and use of arrays.

Each object has an associated lock (S17.13), which is used by synchronized methods (S8.4.3) and the synchronized statement (S14.17) to provide control over concurrent access to state by multiple threads (S17.12, S20.20).


4.3.2 The Class Object

The standard class Object is a superclass (S8.1) of all other classes. A variable of type Object can hold a reference to any object, whether it is an instance of a class or an array (Chapter 10). All class and array types inherit the methods of class Object , which are summarized here and completely specified in S20.1:

package java.lang;


public class Object {
	public final Class getClass() { . . . }
	public String toString() { . . . }
	public boolean equals(Object obj) { . . . }
	public int hashCode() { . . . }
	protected Object clone()
		throws CloneNotSupportedException { . . . }
	public final void wait()

		throws IllegalMonitorStateException,

			InterruptedException { . . . }
	public final void wait(long millis)
		throws IllegalMonitorStateException,
			InterruptedException { . . . }
	public final void wait(long millis, int nanos) { . . . }
		throws IllegalMonitorStateException,
			InterruptedException { . . . }
	public final void notify() { . . . }
		throws IllegalMonitorStateException
	public final void notifyAll() { . . . }
		throws IllegalMonitorStateException
	protected void finalize()
		throws Throwable { . . . }
}

The members of Object are as follows:


4.3.3 The Class String

Instances of class String (S20.12) represent sequences of Unicode characters. A String object has a constant (unchanging) value. String literals (S3.10.5) are references to instances of class String .

The string concatenation operator + (S15.17.1) implicitly creates a new String object.


4.3.4 When Reference Types Are the Same

Two reference types the same type if:



Top© 1996 Sun Microsystems, Inc. All rights reserved.