Reference is a data type or language concept that refers to another value without necessarily exposing a raw memory address.

References are related to pointers, but they are often safer or more abstract. A reference may allow access to a value without allowing pointer arithmetic or direct address manipulation.

For example:

  • Java object variables hold references to objects.
  • Python names refer to objects.
  • Rust has references with borrowing rules.
  • C++ has references as aliases for existing objects.

References are useful for:

  • Avoiding copies
  • Sharing access to objects
  • Passing values to functions efficiently
  • Modeling object identity

Under the hood

At a low level, a reference is often implemented using an address, pointer, handle, or object identifier. The important difference is that the language controls what the program is allowed to do with that underlying representation.

A raw pointer usually exposes the idea of an address directly. A reference often hides that detail behind language rules.

For example, in Java:

User user = new User("Ada");
User other = user;

Both user and other refer to the same object. Under the hood, the variables hold reference values that let the runtime find the object on the heap. The program cannot do pointer arithmetic on those references, and the garbage collector can move or reclaim objects according to the runtime’s rules.

In Python, names behave like references to objects:

items = []
other = items
other.append("Ada")
 
print(items)

Both names refer to the same list object. The assignment copies the reference, not the list itself.

In C++, a reference is closer to an alias:

int value = 42;
int& ref = value;
ref = 100;

After this, value is 100. The reference gives another name for the same object. A compiler may implement this with a pointer internally, but the language treats it differently from a pointer: a reference must be initialized and cannot be reseated to refer to a different object.

In Rust, references are tied to borrowing rules:

let value = String::from("Ada");
let reference = &value;

The reference points at value, but the compiler tracks lifetimes and borrowing rules to prevent dangling references and unsafe mutation.

In Go, there are no C++-style references. Similar behavior is usually modeled with pointers, slices, maps, channels, interfaces, or function parameters depending on the type.

The exact behavior of references depends heavily on the language.

Anki

id: reference-definition deck: Computer Science::Data Types tags: data-types reference

Q: What is a reference? A: A reference is a data type or language concept that refers to another value without necessarily exposing a raw memory address.

id: reference-vs-pointer deck: Computer Science::Data Types tags: data-types reference pointer

Q: How does a reference differ from a raw pointer? A: A reference often hides address details behind language rules, while a raw pointer usually exposes the idea of an address more directly.

id: python-reference-assignment deck: Computer Science::Data Types tags: data-types reference python

Q: In Python, what happens when one variable is assigned to another object variable? A: The assignment copies the reference to the object, not the object itself.