This is a very common misconception. Java does have pointers. Every reference to an object in Java is through a pointer. In this code snipper Object o = new Object(); o is a pointer. What Java doesn't have is pointer arithmetic like C or C++, or any kind of equivalence between pointers and numerical addresses. A Java pointer can point to an object or null, not to an arbitrary location in memory. By removing most of the properties of C/C++ pointers, Java's pointers lost the ability to cause segmentation faults and to corrupt data in memory. You can't have a "stray pointer" in Java, only well-formed ones.
Now, i admit i've never read any of the real technical specification on the language, but... is java REQUIRED to use pointers? I mean, if i wanted to write my own java compiler, if i could figure out a different way of doing it, am i allowed to? IHMO, it would be better to say "java may or may not use pointers, but it doesn't matter, because you can't PROGRAM with them". just like a boolean - i don't know how it's represented inside the JVM, and i don't care. it could be a bit, a byte, a string... whatever. as long as it works, i don't care what it is.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
author and iconoclast
Fred's post is assuming that the definition of a "pointer" is simply that it's a memory cell holding an actual RAM address. Given this definition, then no, Java doesn't use pointers. You could then just say that an object variable in Java is "an object reference". But C++ already has "references," and Java's "references" aren't like the C++ ones, either, because in C++, references can't be reassigned -- only pointers can. So, by the same logic that lets you say that Java doesn't have pointers, you could say Java doesn't have references. Java must have something, though, since you can refer to dynamically allocated objects. Let's call them "snarfblatts." So Java has "snarfblatts", which behave much like C/C++ pointers, except that you can't do pointer arithmetic with snarfblatts, nor can you convert a snarfblatt to an integer, nor can you create a snarfblatt from an integer. The name "snarfblatt" is kind of ugly, and nonintuitive. Let's just call them "Java pointers." Hmmm. Isn't that what I already said?
Yes, Java does have to use references. Do those refrences have to be pointers? Well, not really...but the important thing is that they have to act like pointers, whatever they are. And if they act like pointers, well, then why call them something else? (OK, we call them references because we can't do the pointer arithmetic that you can in C/C++...) Consider:
Now, what is printed out? Since Java uses references, "George Shrinks" is printed. If you did not use references/pointers, you would get "Brian Henderson" printed out.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
author and iconoclast
thanks. i guess it's partially my C background that i hear "pointer", and i think "variable holding memory address". and i never got far with C++, so i got C pointers, C++ references and Java References all muddled in my brain. thanks for the clarification!!!