posted
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 |
posted
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 |
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.
Posts: 1069 | Registered: Feb 2005
| IP: Logged |
posted
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 |
posted
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 |
posted
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) .
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
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 |
posted
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 |