Does java really have pointers or not?

25,942

Solution 1

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.

Solution 2

Internally a reference to an object is implemented as a pointer. There is no pointer arithmetic though...

C++ has pointers because it is built as a superset of C, which does have pointers. C has pointers because it was designed in the 60's. At that time computers had very little memory and pointers allowed the implementation of Strings, arrays and parameter passing.

This is an extract from the White Paper The Java Language Environment:

2.2.9 No More Pointers

Most studies agree that pointers are one of the primary features that enable programmers to inject bugs into their code. Given that structures are gone, and arrays and strings are objects, the need for pointers to these constructs goes away. Thus, Java has no pointer data types. Any task that would require arrays, structures, and pointers in C can be more easily and reliably performed by declaring objects and arrays of objects. Instead of complex pointer manipulation on array pointers, you access arrays by their arithmetic indices. The Java run-time system checks all array indexing to ensure indices are within the bounds of the array.

You no longer have dangling pointers and trashing of memory because of incorrect pointers, because there are no pointers in Java.

Solution 3

Java does have pointers. In fact, everything in Java is pointers (called references).

Exceptions: int, boolean, char, etc.

As everything is pointers, no need for an asterisk to make a difference.

Solution 4

First, the obvious problem is that Java doesn't guarantee that an object stays at the same address throughout its lifetime.

Efficient garbage collectors rely on being able to move objects around, and doing so would invalidate any pointers that (previously) pointed to the object.

Second, Java very nearly does have pointers. C/C++ pointers aren't quite what you think they are.

A C++ pointer does not allow you to just point to an arbitrary memory address and read the contents of it. It does not allow you to increment a pointer past the end of an array.

The language just has no way to detect if you do those things, and no way to catch the error when you do it. But that doesn't mean it's allowed. It's undefined behavior, and in short, could do anything.

Java very nearly does have pointers. Here's a nearly complete list of what you can legally do with a pointer in C++:

  • dereference it to get the pointed-to object
  • reseat it so it points to a different object
  • make it a null pointer
  • if it points into an array, you may perform pointer arithmetics, adding to, or subtracting from the pointer, as long as the result still points into the same array, or taking the difference between two pointers if they both point into the same array.

The last point is really the only one that isn't possible in Java. And it is so mcuh more restricted than people usually think. It doesn't give you a carte blanche to create pointers to any old address. It doesn't let you "access memory directly".

So Java doesn't have pointers because:

  • it would be very challenging to implement correctly into a GC'ed language,
  • it would either require some degree of runtime checking (to guarantee that invalid memory accesses are detected and handled safely), or it would make Java an unsafe language, one where the behavior is not 100% specified,
  • there's just not much point, because pointers allow very little additional functionality, beyond what Java references have

Solution 5

For the same reason java doesn't have unsigned types. The designers of the language decided not to include them in order to keep the language "simpler".

Share:
25,942
Gabriel
Author by

Gabriel

Are you gonna do that ora ora thing?

Updated on July 17, 2022

Comments

  • Gabriel
    Gabriel almost 2 years

    I have looked on google for answers but I am not satisfied.

    My Logic:

    Java uses memory locations, it's just behind the scenes where you can't see or access it (to my knowledge, probably there are ways of accessing them that I don't know).

    My Confusion / Question :

    What is the purpose of not having pointers in a programming language like Java, designed specifically for the internet to be used on any system, vs a programming language like c++, which does use pointers?

    Edit


    Many of you are saying "To keep it simple". If this is the case, then why does a popular programming language, like c++, use pointers anyway?