2

In Java, when I do

Foo bar = new Foo();

bar will be a variable containing the address of the newly constructed object. My professor call it a reference variable

The previous line is equivalent to this in C++:

Foo *bar = new Foo();

here, bar is a pointer to the object

So, is it true that a reference variable in Java is basically a pointer?

Also, when I do this in C++:

Foo bar;

is bar also a pointer? If not, then does that mean there is a difference in memory structure between

Foo bar;

and

Foo *bar = new Foo();

?

1

7 Answers 7

8

Pointers in C++ behave similarly to references in Java.

However, Foo bar; is very different from Foo *bar = new Foo();. The first creates an instance of Foo with automatic duration. It will be destroyed at the end of the block in which it is created. The second creates a dynamically allocated instance of Foo. It will stick around until it is explicitly deleted.

The first can't be used if you want the object to exist after the function returns. If you use a pointer or a reference to it after the function returns, bad things will happen. In that situation, you have to use the second form.

The advantage of the first form is that you don't have to worry about memory management. It will be automatically destroyed at the end of its scope. With the second form, you have to manually delete it, or manage it with a smart pointer of some kind.

Sign up to request clarification or add additional context in comments.

1 Comment

To clarify, the first Foo bar; here is expressed in C++.
3

If you wonder whether the term "pointer" is appropriate for each language, given each language's specification, then yes.

In the Java language specification, Java references are called "pointers":

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.

In the C++ language specification (which is an international ISO standard, the C++ standard), C++ pointers are called "pointers".

1 Comment

Not just in the JLS, either. When you try to dereference a null reference, the exception you get is the much-loved NullPointerException (not NullReferenceException). So I'd say, yes, the designers of Java definitely saw references as pointer-like.
3

So, is it true that a reference variable in Java is basically a pointer?

--> In short YES! But as c++ allow you to manipulate pointers, java does not. (Similar would be good word to differentiate)

Foo bar; // bar is allocated on stack in c++

--> In java this is just declaration of bar of type Foo.

In c++ objects can be saved on stack as well as on heap while in Java objects are stored only on heap while object references pointing to objects are on stack.

Foo *bar = new Foo(); // C++ : *bar is on stack while memory allocated is on heap

Foo bar = new Foo(); // Java : bar on stack and object created using new is on heap

2 Comments

And C++ has references with different semantics to Java ones.
And C++ automatic storage (stack) sometimes goes out the window (pardon the pun) with Microsoft's managed C++ (which I still think someone should be publicly flogged for).
1

Also, when I do this in C++:

Foo bar; is bar also a pointer? If not, then does that mean there is a difference in memory structure between

Foo bar;

It isn't a pointer. It is just an object created on the stack.

In Java all objects are references or pointers, or returns an address using new The difference between C++ pointers and Java "pointers" is that you can't have pointer arithmetic in Java.

And last time I read The Java Programming Language, it says references are pointers.

Comments

1

Well, if you want to be picky they are actually not exactly like pointers:

  1. Pointer arithmetic is not allowed with Java pointers. You cannot increment, decrement, multiply, etc pointers to move what they point to in memory.
  2. Not Strongly typed. In C, you can cast a pointer from one type to pretty much any type wth no consequence (as long as you are careful). In Java, you will get an exception if you try to cast between incompatible types.

Otherwise they behave pretty much the same way as pointers, and as long as you do not need any of the above features of C-style pointers, you will probably not notice a difference.

Comments

1

A pointer is a value that represents a memory address.
A reference is a value that refers to an object.
Pointers are one way to realize references. In fact, there are several ways to use pointers to realize references, because most objects take up more than one address, so you have to define to which address the reference points.

A few differences:

  • You can do math with pointers, because adresses are integer values. A reference value has no inherent meaning, it just represents an object.
  • A pointer can point to any value, eg., a field in an object; a reference always represents the entire object.

Comments

0

It would be more accurate to compare them to Pascal pointers. They have many of the same characteristics: strongly typed, no arithmetic, can be null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.