Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Pointer vs Reference

Reply
Thread Tools

Pointer vs Reference

 
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      09-29-2007
On 2007-09-29 19:38, Summercool wrote:
> On Sep 29, 9:57 am, Joshua Cranmer <(E-Mail Removed)> wrote:
>
>> > Not a pointer, because you use

>>
>> > a.bark()

>>
>> > instead of a->bark() or (*a).bark()

>>
>> Who says that people have to use C-syntax for method dispatches? Since
>> the actual value of the pointer cannot be extracted, why put people
>> through syntactic hoops just to call a method? It *is* a pointer, *by
>> definition.*

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


--
Erik Wikström
 
Reply With Quote
 
 
 
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      09-29-2007
On 2007-09-29 20:06, Summercool wrote:
> On Sep 29, 11:02 am, Summercool <(E-Mail Removed)> wrote:
>> On Sep 29, 10:50 am, Erik Wikström <(E-Mail Removed)> wrote:
>>
>> > The best way to understand the difference between a pointer and a
>> > reference is to not try to compare them. They are two completely
>> > separate concepts.

>
> 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
}

--
Erik Wikström
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      09-29-2007
Summercool wrote:
> 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++.


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-29-2007
* Erik Wikstrm:
> On 2007-09-29 20:06, Summercool wrote:
>> On Sep 29, 11:02 am, Summercool <(E-Mail Removed)> wrote:
>>> On Sep 29, 10:50 am, Erik Wikstrm <(E-Mail Removed)> wrote:
>>>
>>>> The best way to understand the difference between a pointer and a
>>>> reference is to not try to compare them. They are two completely
>>>> separate concepts.

>> 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
> }


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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2007
>Can we confirm the following? also someone said, Java also has
>"reference" like in C++, which is an "implicit pointer":


see http://mindprod.com/jgloss/pointer.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2007
On Sat, 29 Sep 2007 16:57:22 GMT, Joshua Cranmer
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2007
On Sat, 29 Sep 2007 17:38:58 -0000, Summercool
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2007
On Sat, 29 Sep 2007 17:50:41 GMT, Erik Wikstrm
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      09-29-2007
On 2007-09-29 20:30, Mike Schilling wrote:
> Summercool wrote:
>> 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.


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.

--
Erik Wikström
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      09-29-2007
<(E-Mail Removed)> writes:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments