Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Pointer vs Reference

 
 
Summercool
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":

Pointer and Reference
---------------------

I am starting to see what pointer and reference are and how they
relate
to each other.

in the C era, a pointer *is* a reference. that's why when we have

int a = 10;
int *pi = &a;

and you can "dereference it":

*pi = 20;

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
i = 20; // now both a and i are 20

so this type of reference is an implicit pointer... it points to a,
but
you don't use the way in C (int *pi = &a) And when you use (i = 20),
it does the dereference silently. (*pi = 20

so a reference is new: a pointer but "looks like not a pointer".

come to think about it, in Java and Ruby, they are like that too.

a = Car.new

a doesn't look like a pointer, but it is actually a pointer.

we don't dereference it to get to the attributes like (*a).value = 10
or a->value = 10 but just use a.value = 10

So from this point on, a reference and a pointer are not the same... a
reference is a pointer "that doesn't look like a pointer."

they both points to something. but the syntax (or grammar) of usage
doesn't look like it is a pointer in the C era. a reference is an
"automatically dereferenced" pointer, shall we say? or an "implicit"
pointer, or "silent" pointer.

in PHP5,

$a = 10;
$b =& $a; # now $b implicitly points to $a
$b = 20; # now $b implicitly points to $a, which is 20

$a = new Foo("hello"); # $a implicitly points to a Foo object
# the Foo object is 100 bytes,
# but $a is just 4 bytes

$b =& $a; # $b implicitly points to $a.
# $b is a pointer to pointer
# $b points to a four byte pointer, which is $a

$b = new Foo("ok"); # dereference $b and sets its content to
# a new pointer to another object Foo("ok")
# that is, $a points to Foo("ok") now
# $b still points to $a, which points to
Foo("ok")

So now, when you print $b and $a, they are both Foo("ok")


In language we use nowadays, which language has "reference"

In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.

In language we use nowadays, which language has "reference" to a
"reference"?

I think C++, Java, PHP5 do... not sure about Python and Ruby.

 
Reply With Quote
 
 
 
 
Rolf Magnus
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 neither like C++ references nor like C++ pointers. They
are something in between.

> in the C era, a pointer *is* a reference.


Depending on how you define "reference", you could say that.

> Until when C++ comes along, then we have a new "reference":
>
> int a = 10;
> int i =& a; // or int i = &a; i am not sure about the syntax.


It doesn't matter. Whitespace before and after the & operator is ignored.

> i = 20; // now both a and i are 20
>
> so this type of reference is an implicit pointer... it points to a,
> but you don't use the way in C (int *pi = &a) And when you use (i = 20),
> it does the dereference silently. (*pi = 20
>
> so a reference is new: a pointer but "looks like not a pointer".


A reference in C++ is another name for an already existing object. After its
initialization, it behaves just like the object it refers to.

> So from this point on, a reference and a pointer are not the same... a
> reference is a pointer "that doesn't look like a pointer."
>
> they both points to something. but the syntax (or grammar) of usage
> doesn't look like it is a pointer in the C era. a reference is an
> "automatically dereferenced" pointer, shall we say? or an "implicit"
> pointer, or "silent" pointer.


No, it isn't. A pointer can be null, a reference can't. A pointer can be
uninitialized, a reference can't.

> In language we use nowadays, which language has "reference" to a
> "reference"?


That's another thing you can't have in C++. You can have a pointer to
pointer, but you can't have a reference to reference, because references
are not objects (unlike pointers).


 
Reply With Quote
 
 
 
 
Joshua Cranmer
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":
>
> Pointer and Reference
> ---------------------
>
> I am starting to see what pointer and reference are and how they
> relate
> to each other.
>
> in the C era, a pointer *is* a reference. that's why when we have
>
> int a = 10;
> int *pi = &a;
>
> and you can "dereference it":
>
> *pi = 20;
>
> Until when C++ comes along, then we have a new "reference":
>
> int a = 10;
> int i =& a; // or int i = &a; i am not sure about the syntax.


int i = &a; is the syntax. Yes, the ampersand is used for both reference
and address-of, so it can be confusing (especially since I think that i
would be set to the address of a in C).

> i = 20; // now both a and i are 20
>
> so this type of reference is an implicit pointer... it points to a,


I wouldn't call it an implicit pointer: you cannot, to my knowledge,
change i to point to any other object but a like you can with a pointer.
Rather, i is an alias of a: the two objects forever more point to the
same thing.


> so a reference is new: a pointer but "looks like not a pointer".


Not quite. See above.

> come to think about it, in Java and Ruby, they are like that too.


No, it is not. In syntax, Java more nearly follows the reference syntax
but it is closer to a pointer with transparent {de}referencing.

> In language we use nowadays, which language has "reference"
>
> In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.
>
> In language we use nowadays, which language has "reference" to a
> "reference"?
>
> I think C++, Java, PHP5 do... not sure about Python and Ruby.
>


Let me start by asking a question to the C++ language lawyers out there:
what should this print out:

int a = 50, b = 20;
int i = &a;
std::cout << "a: " << a << " b: " << b << " i: " << i;
i = 30;
std::cout << "a: " << a << " b: " << b << " i: " << i;
i = &b;
std::cout << "a: " << a << " b: " << b << " i: " << i;
b = &a;
std::cout << "a: " << a << " b: " << b << " i: " << i;
a = &i; // Creating a circular loop?
std::cout << "a: " << a << " b: " << b << " i: " << i;

Java does not have double referencing or referencing as I understand the
C++ spec (I, unfortunately, no longer have my draft copy of the spec).
Every non-primitive type is closer to a pointer in JVM implementations
(double-pointers, actually, under most implementations). I think PHP5
tends to follow the C++ way. I don't know anything about Ruby and I
think that Python follows the Java style of what's going on.


--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Stuart Golodetz
Guest
Posts: n/a
 
      09-29-2007
"Joshua Cranmer" <(E-Mail Removed)> wrote in message
news:VUtLi.1941$1d2.688@trndny05...
> Summercool wrote:
>> Can we confirm the following? also someone said, Java also has
>> "reference" like in C++, which is an "implicit pointer":
>>
>> Pointer and Reference
>> ---------------------
>>
>> I am starting to see what pointer and reference are and how they
>> relate
>> to each other.
>>
>> in the C era, a pointer *is* a reference. that's why when we have
>>
>> int a = 10;
>> int *pi = &a;
>>
>> and you can "dereference it":
>>
>> *pi = 20;
>>
>> Until when C++ comes along, then we have a new "reference":
>>
>> int a = 10;
>> int i =& a; // or int i = &a; i am not sure about the syntax.

>
> int i = &a; is the syntax.


ITYM int& i = a;

Best wishes,
Stu

<snip>


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-29-2007
[Note to others: crossposted to [comp.lang.c++] and
[comp.lang.java.programmer]].

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


"pointer", "reference", it's just terminology.

What these words mean depends on the programming language.

The special meaning of "reference" in C++ is unfortunate but a fact that
we must live with, it makes it very difficult to discuss these things in
general. Java does not have anything like it.

If you compare only the built-in types, a Java variable of class type, a
Java reference,

SomeType o = new SomeType(); // Java reference

(hope I got the syntax right, it's a long time since I did Java, and I'm
too lazy to put the examples to compilers!)

corresponds most closely to a C++ pointer,

SomeType* p = new SomeType(); // C++ pointer

but there is a huge difference in what you can do, apart from the syntax
issues: C++ supports pointers to pointers, which means that you can
write e.g. a function swap that exchanges the values of two pointer
variables (or any variables), while in Java that level of indirection is
only available for array elements, and then with run-time type checking.

Also, the Java reference provides guaranteed automatic garbage
collection (freeing memory when the object is no longer used), while in
C++ that's not guaranteed, and is in fact not common -- the language
doesn't prevent automatic garbage collection, but doesn't currently
support it either: automatic garbage collection for C++ exists, but is
more like experimental.

The C++ concept corresponding most closely to a Java reference is
therefore a smart pointer, an object that encapsulates a pointer and
provides garbage collection (not general automatic garbage collection
but reference-counted automatic garbage collection, which differs in
that it doesn't automatically deal with cycles of objects),

shared_ptr<SomeType> p( new SomeType() );

but that's not built-in, it's a library solution.

Cheers, & hth.,

- 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
 
Steve Folly
Guest
Posts: n/a
 
      09-29-2007
On 29/9/07 16:09, in article VUtLi.1941$1d2.688@trndny05, "Joshua Cranmer"
<(E-Mail Removed)> wrote:

> Summercool wrote:
>> Can we confirm the following? also someone said, Java also has
>> "reference" like in C++, which is an "implicit pointer":
>>
>> Pointer and Reference
>> ---------------------
>>
>> I am starting to see what pointer and reference are and how they
>> relate
>> to each other.
>>
>> in the C era, a pointer *is* a reference. that's why when we have
>>
>> int a = 10;
>> int *pi = &a;
>>
>> and you can "dereference it":
>>
>> *pi = 20;
>>
>> Until when C++ comes along, then we have a new "reference":
>>
>> int a = 10;
>> int i =& a; // or int i = &a; i am not sure about the syntax.

>
> int i = &a; is the syntax. Yes, the ampersand is used for both reference
> and address-of, so it can be confusing (especially since I think that i
> would be set to the address of a in C).


Wrong. I think what you probably meant is

int& i = a;

Yes, the ampersand is used for reference and address-of - when used as part
of a type, it's a reference, when used in an expression it's the address-of
operator.


> Let me start by asking a question to the C++ language lawyers out there:
> what should this print out:


Compilation error?


--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"

 
Reply With Quote
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      09-29-2007
On 2007-09-29 16:32, Summercool wrote:
> Can we confirm the following? also someone said, Java also has
> "reference" like in C++, which is an "implicit pointer":


Java have references, yes. But they are not the same thing as C++
references, neither when it comes to implementation or semantics.

> Pointer and Reference
> ---------------------
>
> I am starting to see what pointer and reference are and how they
> relate
> to each other.
>
> in the C era, a pointer *is* a reference. that's why when we have


No, a pointer is a pointer. They can be used to implement reference
semantics (as opposed to value semantics).

> int a = 10;
> int *pi = &a;
>
> and you can "dereference it":
>
> *pi = 20;
>
> Until when C++ comes along, then we have a new "reference":
>
> int a = 10;
> int i =& a; // or int i = &a; i am not sure about the syntax.
> i = 20; // now both a and i are 20
>
> so this type of reference is an implicit pointer... it points to a,


No, think about C++ references as aliases for the type they refer to.
One big difference between references and pointers is that references do
not have an address, while pointers do. Consider the following small
program:

int main() {
int a = 5;
int& b = a;
int* c = &a;
}

In a totally un-optimised program there is no guarantee that any more
memory than sizeof(int) + sizeof(int*) is allocated for variables in main().

> but
> you don't use the way in C (int *pi = &a) And when you use (i = 20),
> it does the dereference silently. (*pi = 20
>
> so a reference is new: a pointer but "looks like not a pointer".


One important difference between a pointer and a reference (in C++) is
that a pointer can be made to point at any object of the right type,
while a reference always points to only one object. In other words a
reference can not be reseated.

This is one of the bigger differences between C++ and Java references,
Java references can be reseated and are in that way more like a C/C++
pointer. However notice that a Java reference is not a pointer, it can
not be used to perform pointer arithmetic operations among other
differences.

> come to think about it, in Java and Ruby, they are like that too.
>
> a = Car.new
>
> a doesn't look like a pointer, but it is actually a pointer.
>
> we don't dereference it to get to the attributes like (*a).value = 10
> or a->value = 10 but just use a.value = 10


That probably means that it is a reference, and not a pointer.

> So from this point on, a reference and a pointer are not the same... a
> reference is a pointer "that doesn't look like a pointer."
>
> they both points to something. but the syntax (or grammar) of usage
> doesn't look like it is a pointer in the C era. a reference is an
> "automatically dereferenced" pointer, shall we say? or an "implicit"
> pointer, or "silent" pointer.


I prefer to call it a reference.


[snip PHP which is off topic in both c.l.c++ and c.l.j.p]

> In language we use nowadays, which language has "reference"


I would suspect that any language with OO support provides some kind of
reference, and probably a few others to.

> In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.
>
> In language we use nowadays, which language has "reference" to a
> "reference"?
>
> I think C++, Java, PHP5 do... not sure about Python and Ruby.


C++ does not have references to references, consider the following code:

int a;
int& b = a; // b is a reference to a
int& c = b; // c is a reference to a *not* b

C++ do, however, have pointers to pointers, or references to pointers,
but as previously pointed out pointers and references are entirely
separate things.

--
Erik Wikström
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      09-29-2007
Summercool <(E-Mail Removed)> writes:
>in the C era, a pointer *is* a reference. that's why when we have


In Java, reference values are pointers.

»(...) reference values (...) are pointers«

JLS3, 4.3.1.

http://java.sun.com/docs/books/jls/t...ues.html#4.3.1

via

http://www.purl.org/stefan_ram/pub/jls3

 
Reply With Quote
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      09-29-2007
On 2007-09-29 17:09, Joshua Cranmer wrote:
> Summercool wrote:
>> Can we confirm the following? also someone said, Java also has
>> "reference" like in C++, which is an "implicit pointer":
>>
>> Pointer and Reference
>> ---------------------
>>
>> I am starting to see what pointer and reference are and how they
>> relate
>> to each other.
>>
>> in the C era, a pointer *is* a reference. that's why when we have
>>
>> int a = 10;
>> int *pi = &a;
>>
>> and you can "dereference it":
>>
>> *pi = 20;
>>
>> Until when C++ comes along, then we have a new "reference":
>>
>> int a = 10;
>> int i =& a; // or int i = &a; i am not sure about the syntax.

>
> int i = &a; is the syntax. Yes, the ampersand is used for both reference
> and address-of, so it can be confusing (especially since I think that i
> would be set to the address of a in C).


Actually they are both wrong, but you are party right that the & is the
address-of operator. The above can be written either as

int* i = &a; // i is a pointer

or

int& i = a; // i is a reference

Notice that the & when used for a reference is part of the type (just
like the * for a pointer).

>> i = 20; // now both a and i are 20
>>
>> so this type of reference is an implicit pointer... it points to a,

>
> I wouldn't call it an implicit pointer: you cannot, to my knowledge,
> change i to point to any other object but a like you can with a pointer.
> Rather, i is an alias of a: the two objects forever more point to the
> same thing.
>
>
>> so a reference is new: a pointer but "looks like not a pointer".

>
> Not quite. See above.
>
>> come to think about it, in Java and Ruby, they are like that too.

>
> No, it is not. In syntax, Java more nearly follows the reference syntax
> but it is closer to a pointer with transparent {de}referencing.
>
>> In language we use nowadays, which language has "reference"
>>
>> In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.
>>
>> In language we use nowadays, which language has "reference" to a
>> "reference"?
>>
>> I think C++, Java, PHP5 do... not sure about Python and Ruby.
>>

>
> Let me start by asking a question to the C++ language lawyers out there:
> what should this print out:
>
> int a = 50, b = 20;
> int i = &a;


Assuming you meant i to be a reference here (int& i = a.

> std::cout << "a: " << a << " b: " << b << " i: " << i;


a: 50 b: 20 i: 50

> i = 30;
> std::cout << "a: " << a << " b: " << b << " i: " << i;


a: 30 b: 20 i: 30

> i = &b;


i = b; // Cannot reseat a reference

> std::cout << "a: " << a << " b: " << b << " i: " << i;


a: 20 b: 20 i: 20

> b = &a;


b = i;

> std::cout << "a: " << a << " b: " << b << " i: " << i;


Same as above

> a = &i; // Creating a circular loop?


a = i; // Same as "a = a;"

> std::cout << "a: " << a << " b: " << b << " i: " << i;


Same as above

--
Erik Wikström
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      09-29-2007
Rolf Magnus wrote:

>> int i =& a; // or int i = &a; i am not sure about the syntax.

>
> It doesn't matter. Whitespace before and after the & operator is ignored.


Oops. Even though my answer is right, this is - as others have pointed out -
not the correct syntax for defining a reference.

 
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