Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a reference in Java?

Tags:

java

reference

What exactly is a reference in Java? Is it a memory address? Is a Java reference the equivalent of a dereferenced C++ pointer?

In other words, given the following:

Object o1 = new Object();
Object o2 = new Object();

o1 == o2

Is the above comparison the equivalent of comparing two pointers in C++?

like image 511
auser Avatar asked Sep 24 '12 12:09

auser


People also ask

What is a reference class in Java?

Reference Class is an abstract base class for reference object. This class contains methods used to get information about the reference objects. This class is not a direct subclass because the operations on the reference objects are in close co-operation with the garbage collector.

What is reference data type in Java example?

Examples of reference data types are class, Arrays, String, Interface, etc. Examples of primitive data types are int, float, double, Boolean, long, etc. JVM allocates 8 bytes for each reference variable, by default. Its size depends on the data type.

What does a reference value do in Java?

For reference types, Java passes the reference by value. That is, it creates a copy of the pointer to the object in memory.

What is a reference object?

An object reference is information on how to find a particular object. The object is a chunk of main memory; a reference to the object is a way to get to that chunk of memory. The variable str does not actually contain the object, but contains information about where the object is.


2 Answers

o1 == o2 is pretty much equivalent to comparing two pointers in C/C++, yes.

But there are a two main differences between references in Java and pointers in C/C++ that are quite important:

  • Java references can't do pointer arithmetic: you can't "add 3" to a reference, you can only let it point to another (known) object
  • Java references are stongly typed: you can't "reinterpret" what lies on the other end of a reference unless you reinterpret it as a type that that object actually is.

Also a short note about the word "reference": C++ has references that act quite differently from both pointers in C and references in Java (but I don't know enough about C++ to tell you the specifics).

For a thorough discussion of this, see this related question on programmers.SE.

like image 95
Joachim Sauer Avatar answered Sep 28 '22 06:09

Joachim Sauer


What exactly is a reference in Java?

It is an index of an object. It can be thought of as like a pointer but it differs in the fact that it

  • can change at any time.
  • does not always have a direct relationship with memory addresses.
  • is usually 32-bit in a 64-bit JVM.
  • you can't re-interpreter what the reference refers to. You can only change the type of the reference itself.

Is the above comparison the equivalent of comparing two pointers in C++?

Yes.


On Compresses Oops which allows a 64-bit JVM to sue 32-bit references.

Java HotSpot™ Virtual Machine Performance Enhancements - Compressed Oops

Compressed oops in the Hotspot JVM

IBM V6 - More effective heap usage using compressed references

like image 29
Peter Lawrey Avatar answered Sep 28 '22 05:09

Peter Lawrey