Re: Pointer vs Reference



Erik Wikström wrote:
Foo f1 = new Foo();
Foo f2 = f1;

While it does not exactly take the address of the object referred to by
f1, it does create a new reference (pointer) that points to the same object.

Roedy Green wrote:
In assembler, what that does in allocate space for a new Foo object on
the heap somewhere. It puts the 32-bit address of that object in f1, a
local variable allocated on the stack frame.

Then it copies the pointer value from f1 and stores it in f2.

No reference is created, except on the initial allocation.

Oh, I see, I was under the impression that f1 was a reference. Just out

f1, f2 are variables that hold references.

of curiosity what is a reference in Java? Does f1 (and later f2) point
to a reference which refers to the Foo object, or is the Foo object the
reference (which would be quite counter intuitive)?

A reference is a pointer is a reference is a pointer ... in Java. A reference variable holds a reference, which is a "magic arrow that points to a thing", in some sense an address of that thing (that does not translate to a unique number unchanged over the lifetime of the object). Whether you think of f1 and f2 as two magic arrows that look exactly the same and point to the same thing, or as two variables that hold copies of the exact same magic arrow that points to a thing is a matter of conceptual approximation and your choice.

If you have two pieces of paper that have printed on them, "123 Main Strasse, Gewürtztraminer, Bavaria", is that two pieces of paper that hold the same address (reference), or that hold two copies of the same address?

Was a new reference created when the contents of one piece of paper were copied to the other? Certainly no new address was created, in the sense that the location is the same in both cases. If you think of the "reference" as the information in the variable, you could argue that it's not a different reference but the same reference in a different variable.

"piece of paper" -> "variable"
"123 Main Strasse,..." -> "reference" or "pointer"
"the Led Zeppelin residence" -> "object"

--
Lew
.



Relevant Pages

  • Re: 7.0 wishlist?
    ... The "auto-implement" is intended mainly for the odd situation where an existing class you can't edit fits some interface and you're willing to take responsibility for it if it turns out not to actually adhere to the contract, and try using it where that interface type is expected. ... If reference declarations started showing up with the odd asterisk, bang, or other punctuation mark on it, but never primitive declarations, people would probably be able to guess what was going on, on the basis of "what other binary flag might be set on references but not primitives and would be really useful besides can be/cannot be null?" ... the compiler cannot prove by static analysis that the RHS isn't null might be a good idea. ... Object foo, bar; ...
    (comp.lang.java.programmer)
  • Re: 7.0 wishlist?
    ... This is a slight modifier that would be very common on reference declarations, so it would be good to economize on typing or visual clutter where it was used. ... which has the enum constants actually singleton instances of same-named subclasses. ... enum Foo ... The point being to allow to publish an interface ...
    (comp.lang.java.programmer)
  • Re: C++ design question
    ... >>void DoStuffWithBarBase ... I assumed that the Foo subclasses each had specialized responsibilities, ... policies for object and relationship instantiation (object ... conditional one can't implement it with a reference. ...
    (comp.object)
  • Re: A doubly linked-list in C
    ... The pointer is being passed ... causes the object to be passed by reference. ... int foo; bar; passes foo but bar cannot update it. ... The mechanism is an implementation detail, ...
    (comp.lang.c)
  • Re: Pointer and reference
    ... Use references unless you must use a pointer. ... The C++ keyword const implies a variable cannot ... by reference. ... Now if foo() won't change that city's value, ...
    (comp.lang.cpp)