Pointer vs Reference

G

Guest

Now... so in Java, Python, PHP5, and Ruby, it looks like they are all
pointers when you say

a = new Dog("woofy")

or

a = Dog.new("woofy")

so they are all pointers, not reference. (because reference cannot
point to a different thing after it is set, like a = new Dog("lulu")
or a = nil)

I do not know, but at least in Java they are references, and I think
that they use the same terminology in the other languages you mentioned.
the syntax a.color or a.bark is just a simpler way of writing C or C
++'s "->"

Not necessarily, that depends on the semantics used in the language in
question. Just a guess but I would suspect that there are subtle
differences between references in all those languages.
so that's it? I tend to compare the "." and the "->" as I view the
relatively modern language having similar syntax or operators...
(didn't expect "." to mean "->" in another language)

Please try to separate different languages. Just because some concepts
have the same name in more than one language and share some
commonalities does not mean that they are the same thing or that they
can be easily explained in terms of some other language (unless you go
down to assembly level.

Just realise that a C/C++ pointer is not a C++ reference is not a Java
reference is not a C# reference, and so on. A Java reference is a Java
reference, nothing more nothing less, just like a C++ reference is a C++
reference, not a pointer or something else, it is just a reference.
 
G

Guest

here is what C++ in a Nutshell says for reference:

2.6.2.5 References

A reference is a synonym for an object or function. A reference is
declared just like a pointer, but with an ampersand (&) instead of an
asterisk (*). A local or global reference declaration must have an
initializer that specifies the target of the reference. Data members
and function parameters, however, do not have initializers. You cannot
declare a reference of a reference, a reference to a class member

The last part is not quite right, consider this:

struct Foo {
int i;
};

int main() {
Foo f;
int& ir = f.i; // Reference to class member
}
 
M

Mike Schilling

Summercool said:
Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":

Java references are like C++ pointers, with a few exceptions:

1. Java does not have pointer arithmetic.
2. Java cannot take the address of an existing object; pointers are created
only via the "new" operation.

In fact, Java pointers are almost exactly the same as Pascal pointers.

But Java references are nothing like C++ references, which

1. Cannot be reset.
2. Cannot be null.

This is all clearer if you realize that "." is Java is the same as "->" is
C++.
 
A

Alf P. Steinbach

* Erik Wikström:
The last part is not quite right, consider this:

struct Foo {
int i;
};

int main() {
Foo f;
int& ir = f.i; // Reference to class member
}

Also, "function parameters ... do not have initializers", well, I guess
the author(s) could argue that they're talking about template
parameters, but I would not be convinced by such attempt at weaseling.

int def;
void foo( int const& x = 1234 ) {}
void bar( int& x = def ) {}

In short, based on this short extract from the book, C++ in a Nutshell
doesn't seem to be accurate or correct about details.

It may be great for getting an overall idea, though.

Cheers,

- Alf
 
R

Roedy Green

In Java, it really is a pointer. The JLS actually specifically says that
reference values are pointers, as Stefan pointed out.

In Java I believe the official definition of pointer is "a non-null
reference". However, nearly every Java programmer uses the term to
mean a low level machine pointer like C++ uses with manual referencing
and almost no restrictions on what you can do with it -- e.g. assign
it numeric values, do arithmetic, compose it with bit operators etc.

References are pointers with airbags.
 
R

Roedy Green

so they are all pointers, not reference. (because reference cannot
point to a different thing after it is set, like a = new Dog("lulu")
or a = nil)

In Java a final reference cannot be changed. An non-final reference
can.
 
R

Roedy Green

The best way to understand the difference between a pointer and a
reference is to not try to compare them.

The best I think you could get is a difference within each language,
Java/C++ etc of the difference.
 
G

Guest

Java references are like C++ pointers, with a few exceptions:

1. Java does not have pointer arithmetic.
2. Java cannot take the address of an existing object; pointers are created
only via the "new" operation.

It was some time since I last used Java, but would not something like
this work?

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.
 
S

Stefan Ram

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.

In Java terminology, it creates a new /variable/ f2 that
contains /the same/ reference value as the variable f1.

Many terms, like »variable« and »object«, have different
meanings in Java and C++, which are specifed in the JLS3 and
in ISO/IEC 14882, respectively.
 
L

Lew

Stefan said:
In Java terminology, it creates a new /variable/ f2 that
contains /the same/ reference value as the variable f1.

Many terms, like »variable« and »object«, have different
meanings in Java and C++, which are specifed in the JLS3 and
in ISO/IEC 14882, respectively.

For that matter, "address" doesn't have the meaning in Java that most people
seem to imagine. For one thing, it's not a number.

I interpret the metaJava [1] word "address" as "magical arrow pointing to the
thing".

[1] I coin "metaJava" to mean "the language in which one speaks about Java".
So this footnote must be metametaJava, "the language in which one speaks about
the language in which one speaks about Java".
 
R

Roedy Green

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.

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.

References may also be implemented as handles rather than direct
pointers. See http://mindprod.com/jgloss/reference.html
http://mindprod.com/jgloss/pointer.html
 
R

Roedy Green

I interpret the metaJava [1] word "address" as "magical arrow pointing to the
thing".

The JVM leaves tremendous leeway in how addresses, pointers and
references are actually implemented. It is difficult to talk about
what Java does without something nailed down and concrete about what
MUST be happening at the machine code level.
 
M

Mike Schilling

Erik said:
It was some time since I last used Java, but would not something like
this work?

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.

I would say (somewhat pedantically) that only the new operation "creates" a
pointer; your second line copies an existing pointer.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Summercool said:
Now... so in Java, Python, PHP5, and Ruby, it looks like they are all
pointers when you say

a = new Dog("woofy")

or

a = Dog.new("woofy")

so they are all pointers, not reference. (because reference cannot
point to a different thing after it is set, like a = new Dog("lulu")
or a = nil)

True, if you use C++ terminology.

False, if you use Java terminology.

In general I will recommend using the languages own terminology.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Roedy said:
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.

N bit address - 32 bit on some platforms.

Arne
 
L

Lew

Roedy said:
[In Java,] It puts the 32-bit address of that object in ... a
local variable allocated on the stack frame.
N bit address - 32 bit on some platforms.

N.b., I know I've said it before, but in Java any particular N-bit pattern
associated with the object as an address can change during the lifetime of the
object, and in fact can be optimized away entirely during runtime. An address
in Java is a GUID for an object, but it doesn't perform like a number as C/C++
programmers are wont to understand addresses. The mental model of an
"address" is very different between Java and C/C++.
 
M

Mike Schilling

Lew said:
Roedy said:
[In Java,] It puts the 32-bit address of that object in ... a
local variable allocated on the stack frame.
N bit address - 32 bit on some platforms.

N.b., I know I've said it before, but in Java any particular N-bit
pattern associated with the object as an address can change during
the lifetime of the object, and in fact can be optimized away
entirely during runtime. An address
in Java is a GUID for an object, but it doesn't perform like a number as
C/C++
programmers are wont to understand addresses. The mental model of an
"address" is very different between Java and C/C++.

The same is true in C++, though it's perhaps less obvious. There are
garbage collectors for C++, some of which compact as well as collect. It's
also possible for the value of a C++ pointer not to be a machine address,
even though that's by far the most common implementation.

If what you're saying is that most C++ programmers either don't know this or
program as if they don't, we're entirely in agreement.,
 
G

Guest

Lew said:
Roedy said:
[In Java,] It puts the 32-bit address of that object in ... a
local variable allocated on the stack frame.
N bit address - 32 bit on some platforms.

N.b., I know I've said it before, but in Java any particular N-bit
pattern associated with the object as an address can change during the
lifetime of the object, and in fact can be optimized away entirely
during runtime. An address in Java is a GUID for an object, but it
doesn't perform like a number as C/C++ programmers are wont to
understand addresses. The mental model of an "address" is very
different between Java and C/C++.

It may change.

But eventually some code will use an address into memory.

My point was that the address is not necessary a 32 bit address.

Arne
 
J

James Kanze

In Java, reference values are pointers.
[/QUOTE]
All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to (provided that Java supports operator
overloading, I can not remember if it does).

Alf really got it right when he said that it's just arbitrary
terminology, and that each language has its own definitions.
Never the less: the difference between pointers (called
references) in Java, and pointers in C++, is that in C++, a
pointer is, itself, an object in its own right. Unlike Java,
C++ doesn't divide its types into two incompatible categories;
an object can have any type you want, any type may support copy,
operators, etc., and you can take the address of any type. And
pointers are just another object type, with no particular
special rules. All of the rest (pointers to pointers, etc.)
follows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top