FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » Pointers - Computer Science

   
Author Topic: Pointers - Computer Science
Alcon
Member
Member # 6645

 - posted      Profile for Alcon   Email Alcon         Edit/Delete Post 
Query: Does Java have pointers? Or are those references?

I hold that what Java uses are references, as does one of my text books and Wikipedia. However, much of the rest of the staff here at CAEN insist that it has pointers and that pointers and references are basically the same thing.

What do you think?

Posts: 3295 | Registered: Jun 2004  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Well - technically, the only difference is that a reference cannot be null, it always points to a valid object. So Java has pointers. But I feel the distinction is rather meaningless in a language that only has one of the two.
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Swampjedi
Member
Member # 7374

 - posted      Profile for Swampjedi   Email Swampjedi         Edit/Delete Post 
Java references can be null, IIRC.

You can't create a pointer to a primitive, so I dunno. I think it's all just semantics. You can't do crazy pointer arithmetic like in C and break your program, though. [Big Grin]

Posts: 1069 | Registered: Feb 2005  |  IP: Logged | Report this post to a Moderator
Will B
Member
Member # 7931

 - posted      Profile for Will B   Email Will B         Edit/Delete Post 
Underneath the hood, every Java variable is a pointer, but Java doesn't have pointers where the programmer can get at them. Which sure prevents a lot of pointer errors.

And here I was thinking this would be a thread giving pointers on CS.

Posts: 1877 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
Swampjedi
Member
Member # 7374

 - posted      Profile for Swampjedi   Email Swampjedi         Edit/Delete Post 
Here's a *pointer for you. Malloc then free, malloc then free! [Big Grin]
Posts: 1069 | Registered: Feb 2005  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
As far as behavior, they behave far more like pointers. After all, method arguments are java variables, and they pass by value. That is, this code fragment doesn't work:
code:
public void doStuff(Object x) {
x = new Object();
}

public static void main(String[] args) {
Object y = new Object();
z = y;
doStuff(y);
assert y != z;
}

If x were a reference, then that would work. Notice that this shows a difference between pointers and references other than nullability. However, in java non-primitive variables are called references, despite generally behaving like pointers that have been passed by value (everything in java is pass by value).

Now, there's a way they aren't like pointers, and that's that they aren't sometimes. The JVM isn't required to implement them as pointers, just give them certain behaviors. It could easily optimize them to be references where it knows that won't matter, or use pointers to pointers, or whatever. Java's not as close to the metal as C++.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
My overall point is sound, but there's actually an error in my argument above, amusingly.

It wouldn't be too hard to get the code working to properly demonstrate my argument, but it would be mildly annoying. Bonus points for anyone who figures out the issue (no need to post a code sample, just an explanation is fine) [Smile] .

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
sarahdipity
Member
Member # 3254

 - posted      Profile for sarahdipity   Email sarahdipity         Edit/Delete Post 
Oh, here's a *fantastic* link about pointers. It's cute and funny.

http://cslibrary.stanford.edu/104/

Posts: 872 | Registered: Mar 2002  |  IP: Logged | Report this post to a Moderator
Swampjedi
Member
Member # 7374

 - posted      Profile for Swampjedi   Email Swampjedi         Edit/Delete Post 
First, where the heck does z come from?

Second, you're calling doStuff from a static context.

Posts: 1069 | Registered: Feb 2005  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
Sorry, typing quickly; Obviously z is Object z, and doStuff would need to be static. Those have nothing to do with the logic of the situation.

The problem has to do with two wrongs making a right, sort of, were java to use references in the C++ sense.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Well, if you were in C++, then z would point to whatever address y pointed to. When you come back from doStuff, z is still pointing to whatever y points to, which is now a new Object. (In C++, that is, not in Java.) So the assertion will still fail.

There a trivial workaround for this in Java, though, if you insist on passing by reference; just wrap your object in an Object[].

Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
KoM got it [Smile] .
Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2