---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 11 Edition"
java_version: 11
chapter: "Chapter 4. Types, Values, and Variables"
section: "4.3"
heading: "4.3. Reference Types and Values"
parent: "Chapter 4. Types, Values, and Variables"
source: https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.3
id: "4.3"
url: https://jvm.obfuscated.software/jls/se11/4.3.html
raw: https://jvm.obfuscated.software/jls/se11/4.3.md
---

# 4.3. Reference Types and Values

There are four kinds of *reference types*: class types ([§8.1](8.1.md)), interface types ([§9.1](9.1.md)), type variables ([§4.4](4.4.md)), and array types ([§10.1](10.1.md)).

```
ReferenceType:
  ClassOrInterfaceType
  TypeVariable
  ArrayType

ClassOrInterfaceType:
  ClassType
  InterfaceType

ClassType:
  {Annotation} TypeIdentifier [TypeArguments]
  PackageName . {Annotation} TypeIdentifier [TypeArguments]
  ClassOrInterfaceType . {Annotation} TypeIdentifier [TypeArguments]

InterfaceType:
  ClassType

TypeVariable:
  {Annotation} TypeIdentifier

ArrayType:
  PrimitiveType Dims
  ClassOrInterfaceType Dims
  TypeVariable Dims

Dims:
  {Annotation} [ ] {{Annotation} [ ]}
```

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`.

A class or interface type consists of an identifier or a dotted sequence of identifiers, where each identifier is optionally followed by type arguments ([§4.5.1](4.5.1.md)). If type arguments appear anywhere in a class or interface type, it is a parameterized type ([§4.5](4.5.md)).

Each identifier in a class or interface type is classified as a package name or a type name ([§6.5.1](6.5.1.md)). Identifiers which are classified as type names may be annotated. If a class or interface type has the form `T.id` (optionally followed by type arguments), then `id` must be the simple name of an accessible member type of `T` ([§6.6](6.6.md), [§8.5](8.5.md), [§9.5](9.5.md)), or a compile-time error occurs. The class or interface type denotes that member type.

## 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](15.9.md)).

An array is explicitly created by an array creation expression ([§15.10.1](15.10.1.md)).

Other expressions may implicitly create a class instance ([§12.5](12.5.md)) or an array ([§10.6](10.6.md)).

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

        /* 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](6.6.md)) or a field access expression ([§15.11](15.11.md))
- Method invocation ([§15.12](15.12.md))
- The cast operator ([§5.5](5.5.md), [§15.16](15.16.md))
- The string concatenation operator `+` ([§15.18.1](15.18.1.md)), 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](15.20.2.md))
- The reference equality operators `==` and `!=` ([§15.21.3](15.21.3.md))
- The conditional operator `? :` ([§15.25](15.25.md)).

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.

**Example 4.3.1-2. Primitive and Reference Identity**

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

This program 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 ([§4.12.3](4.12.3.md)) in the one `Value` object created by the only `new` expression, while `i1` and `i2` are different variables.

Each object is associated with a monitor ([§17.1](17.1.md)), which is used by `synchronized` methods ([§8.4.3](8.4.3.md)) and the `synchronized` statement ([§14.19](14.19.md)) to provide control over concurrent access to state by multiple threads ([§17 (*Threads and Locks*)](17.md)).

## 4.3.2. The Class `Object`

The class `Object` is a superclass ([§8.1.4](8.1.4.md)) of all other classes.

All class and array types inherit ([§8.4.8](8.4.8.md)) the methods of class `Object`, which are summarized as follows:

- The method `clone` is used to make a duplicate of an object.

- The method `equals` defines a notion of object equality, which is based on value, not reference, comparison.

- The method `finalize` is run just before an object is destroyed ([§12.6](12.6.md)).

- The method `getClass` returns the `Class` object that represents the class of the object.

  A `Class` object exists for each reference type. It can be used, for example, to discover the fully qualified name of a class, its members, its immediate superclass, and any interfaces that it implements.

  The type of a method invocation expression of `getClass` is `Class<?` `extends` |T|`>`, where T is the class or interface that was searched for `getClass` ([§15.12.1](15.12.1.md)) and |T| denotes the erasure of T ([§4.6](4.6.md)).

  A class method that is declared `synchronized` ([§8.4.3.6](8.4.3.6.md)) synchronizes on the monitor associated with the `Class` object of the class.

- The method `hashCode` is very useful, together with the method `equals`, in hashtables such as `java.util.HashMap`.

- The methods `wait`, `notify`, and `notifyAll` are used in concurrent programming using threads ([§17.2](17.2.md)).

- The method `toString` returns a `String` representation of the object.

## 4.3.3. The Class `String`

Instances of class `String` represent sequences of Unicode code points.

A `String` object has a constant (unchanging) value.

String literals ([§3.10.5](3.10.5.md)) are references to instances of class `String`.

The string concatenation operator `+` ([§15.18.1](15.18.1.md)) implicitly creates a new `String` object when the result is not a constant expression ([§15.28](15.28.md)).

## 4.3.4. When Reference Types Are the Same

Two reference types are the *same compile-time type* if they are declared in compilation units associated with the same module ([§7.3](7.3.md)), and they have the same binary name ([§13.1](13.1.md)), and their type arguments, if any, are the same, applying this definition recursively.

When two reference types are the same, they are sometimes said to be the *same class* or the *same interface*.

At run time, several reference types with the same binary name may be loaded simultaneously by different class loaders. These types may or may not represent the same type declaration. Even if two such types do represent the same type declaration, they are considered distinct.

Two reference types are the *same run-time type* if:

- They are both class or both interface types, are defined by the same class loader, and have the same binary name ([§13.1](13.1.md)), in which case they are sometimes said to be the *same run-time class* or the *same run-time interface*.
- They are both array types, and their component types are the same run-time type ([§10 (*Arrays*)](10.md)).
