Does java really have pointers or not? [closed]

The simple answer is that it is a design decision. The slightly longer answer is that pointers in C++ are only necessary for memory manipulation, which is not a concept that applies to Java (see below).

Java envisions a fairly systematic, object-oriented programming model, and all class-based types are essentially always handled through a pointer, and so this fact isn’t exposed to the user at all.

Raw pointers in C++ aren’t very necessary in high-quality, systematic and idiomatic programming, either. What raw pointers do allow you to do (namely pointer arithmetic) is generally considered “unsafe”, and so it is simply left out of Java altogether.

Note by the way that many things people attempt to do with pointers in C and C++ is actually undefined behaviour. Using pointers correctly leaves you with a fairly restricted set of options, most of which can be done better in idiomatic C++.

About the only real use for pointers is direct memory manipulation. Since Java doesn’t want you to do that (and in fact its garbage-collected memory management would actively interfere with and be broken by manual memory manipulation), there’s no need for explicit pointers.

After your update: The last paragraph is (in my opinion) the most striking explanation: In C++ you need to be able to write your own memory managing code (cf. “allocators”). And memory has to be handled via pointers. So my strict answer is that you need pointers in C++ when you’re implementing the memory management, and never otherwise.

Leave a Comment