Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Java uses the word reference for two related ideas: categories in the type system and objects that differ in how they affect garbage collection. The type system has four reference-type categories—class, interface, type variable, and array. Separately, Java’s java.lang.ref API provides soft, weak, and phantom reference objects; ordinary variable access is commonly called a strong reference.

Keeping those meanings separate makes the distinction clear: a reference type describes what kind of type a declaration uses, while a strong, soft, weak, or phantom reference describes an object’s reachability and possible cleanup behavior.

What does “reference” mean in Java?

A reference value refers to an object; it is not the object itself. For example, in String name = "Ada";, String is a class type, name is a variable holding a reference value, and the string instance is the object. The Java Language Specification for Java SE 22 states that the values of a reference type are references to objects. See JLS §4.3.

That language also appears in garbage-collection discussions, where strong, soft, weak, and phantom describe how an object can be reached. These are not four additional Java type-system categories.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What are the four reference types in Java?

The Java Language Specification, Java SE 22, classifies reference types into four kinds: class types, interface types, type variables, and array types.

  • Class types: Types declared by classes, such as String.
  • Interface types: Types declared by interfaces, such as Runnable.
  • Type variables: Generic type parameters, such as T in List<T>.
  • Array types: Types for arrays, such as int[] or String[].

An array is a reference type even when its elements are primitive values. In int[] scores, the array object is accessed through a reference, while each element stores an int primitive value.

What are strong, soft, weak, and phantom references?

These names describe reachability, not the language’s type categories. Oracle’s Java SE 25 java.lang.ref package documentation describes three reference-object types—soft, weak, and phantom—each weaker than the last. Strong references are the ordinary baseline rather than a special reference-object class.

Kind Reachability What it means in practice
Strong The object can be accessed without traversing a Reference object. Ordinary assignments such as Object value = new Object(); provide this familiar access path. The object remains reachable while such a path remains usable.
Soft The object is not strongly reachable, but can be reached through a soft reference. Intended for memory-sensitive caches. The garbage collector decides when to clear the reference in response to memory demand; it does not promise a particular expiration time.
Weak The object is neither strongly nor softly reachable, but can be reached through a weak reference. Useful for mappings that should not keep keys or values alive, including canonicalizing mappings.
Phantom The object is neither strongly, softly, nor weakly reachable; the API definition also describes it as finalized. Supports post-mortem cleanup coordination and notification, typically through a ReferenceQueue or the Cleaner facility.

How do the four reachability kinds differ?

Strong references: ordinary object access

A normal local variable, field, or array element containing an object reference is typically a strong path. While the program can reach the object through such a path, it is strongly reachable. Strong references are the default in everyday Java code; there is no special wrapper needed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Soft references: collector-managed cache retention

A soft reference does not keep an object strongly reachable. The collector may clear it when responding to memory demand, which is why the API identifies memory-sensitive caches as a use. Clearing is at the collector’s discretion, not a deterministic cache-expiration mechanism. Do not rely on a soft-referenced value surviving until a particular memory threshold or being collected at a particular time.

Weak references: non-retaining associations

A weak reference does not keep its referent alive once stronger forms of reachability are absent. This is useful when an association should not prevent its key or value from being reclaimed. Weak does not mean immediate: collection, reference clearing, and any queue notification are not synchronous scheduling guarantees.

Phantom references: cleanup coordination

A phantom reference is for learning about an object’s post-mortem reachability state and coordinating cleanup, not for retrieving the object for normal use. The java.lang.ref API identifies Cleaner as a managed cleanup facility and supports phantom-reference workflows. Do not treat finalization as the recommended way to release application resources.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do ReferenceQueue and Cleaner fit in?

A program can register a soft, weak, or phantom reference with a ReferenceQueue. After the collector detects the relevant reachability change, it clears the reference and adds it to the queue at some later time. Queue processing lets code notice that a reference has been cleared, but it does not provide a precise deadline.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Cleaner provides a managed facility for cleanup actions associated with objects that become phantom reachable. It is relevant when cleanup must be coordinated after ordinary access paths have disappeared; it does not make a phantom reference a way to get the object back.

Reference type, reference variable, and reference object: what is the difference?

  • A reference type is a type-system category, such as a class, interface, type variable, or array type.
  • A reference variable is a variable whose value can refer to an object, such as name in String name = "Ada";.
  • A reference object is an instance of an API class in java.lang.ref, such as a WeakReference or SoftReference, which holds a referent with special reachability behavior.

The terms answer different questions: the first asks what kind of type is declared, the second asks where an object reference value is stored, and the third asks whether the API wrapper changes how the referent is considered reachable.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.