This is topic Pointers - Computer Science in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=043578

Posted by Alcon (Member # 6645) on :
 
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?
 
Posted by King of Men (Member # 6684) on :
 
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.
 
Posted by Swampjedi (Member # 7374) on :
 
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]
 
Posted by Will B (Member # 7931) on :
 
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.
 
Posted by Swampjedi (Member # 7374) on :
 
Here's a *pointer for you. Malloc then free, malloc then free! [Big Grin]
 
Posted by fugu13 (Member # 2859) on :
 
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++.
 
Posted by fugu13 (Member # 2859) on :
 
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] .
 
Posted by sarahdipity (Member # 3254) on :
 
Oh, here's a *fantastic* link about pointers. It's cute and funny.

http://cslibrary.stanford.edu/104/
 
Posted by Swampjedi (Member # 7374) on :
 
First, where the heck does z come from?

Second, you're calling doStuff from a static context.
 
Posted by fugu13 (Member # 2859) on :
 
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.
 
Posted by King of Men (Member # 6684) on :
 
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[].
 
Posted by fugu13 (Member # 2859) on :
 
KoM got it [Smile] .
 


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