5

I read quite a few resources on the internet and now I am quite confused about the existence of pointers in Java.

Some claim that there are pointers and some claim that there is no such concept. So what is the truth?

Mat
  • 2,066
  • 2
  • 26
  • 31
log N
  • 487
  • 2
  • 7
  • 13

2 Answers2

17

At face value, since the official Java language specification specifically mentions pointers, then the answer is YES, pointers exist in Java.

So how do we explain the apparent contradictory opinions here? Well, like many issues, the disagreement comes down to semantics.

The term "pointer" is strongly associated with the C/C++ concept of pointers, which are variables which explicitly store memory addresses, and which can be modified arithmetically to point to any arbitrary address. In Java, pointers exist, but not as a first-class language concept. Rather, pointers only exist as an implementation detail for References. The spec says:

An object is a class instance or an array.

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

It's also telling that accessing a null reference throws a NullPointerException, not a NullReferenceException. So, clearly the Java designers were at least thinking in terms of pointers when they wrote the Java language spec. (Although really it should be called a NullReferenceException; in my opinion the name of that exception leaks through the abstraction)

So, the answer is that Java References are "pointer-ish", in the sense that they are implemented in terms of pointers (just like most implementations of C++ references), but Java References provide a higher-level abstraction which restricts the programmer from manipulating the actual memory address. Syntactically, a Java reference only gives you access to the members or methods of the pointed-to Object - not the memory location of the Object.

So yes, pointers exist in Java, for a certain value of "exist". But it's probably not helpful to speak in terms of "pointers" when discussing Java language concepts.

Charles Salvia
  • 7,342
  • 1
  • 35
  • 33
8

Java does have pointers. Any time you create an object in Java, you're actually creating a pointer to the object; this pointer could then be set to a different object or to null, and the original object will still exist (pending garbage collection).

What you can't do in Java is pointer arithmetic. You can't dereference a specific memory address or increment a pointer.

If you really want to get low-level, the only way to do it is with the Java Native Interface; and even then, the low-level part has to be done in C or C++.

nmkyuppie
  • 408
  • 2
  • 10
  • 3
    Those are not pointers. Pointer arithmetics and memory management are defining aspects of pointer. Without it, it is not a pointer. – Euphoric Aug 05 '13 at 06:30
  • @Euphoric You can do some kind of pointer arithmetic also, by using `Unsafe`; but as the name suggests, is it unsafe: http://mishadoff.github.io/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/ . – Random42 Aug 05 '13 at 06:50
  • 3
    @Euphoric: By that logic, C++'s smart pointers are not pointers either. And at CPU level, there's no pointer arithmetic either - there's just arithmetic. So, aren't you arguing here that the only pointers are pointers like C has them? Because obviously pointers in Java are unlike C. – MSalters Aug 05 '13 at 08:37
  • 6
    @Euphoric: Yes, it very much is. C doesn't have a monopoly on defining terms. – Michael Borgwardt Aug 05 '13 at 12:01
  • 2
    I agree with @Euphoric to the extent that there is a semantic difference between pointers and references (and I don't mean these terms in the strict sense that C++ understands them): Pointers are a low-level concept usually involving memory addresses, with which you can do arithmetic. References, OTOH, are just a semantic concept that may be implemented using pointers. I would argue that Java as a language is defined in terms of references, and that any particular Java implementation may implement references with pointers. – stakx Aug 05 '13 at 13:07
  • @MichaelBorgwardt Pointers were invented before C existed and the term really isn't C specific but as it is used it always has some connection to memory addresses. Just use the term 'reference' when talking about this concept in Java there are too many differences. – Honza Brabec Aug 05 '13 at 13:12
  • @Euphoric: Some languages (e.g. Pascal) have pointers without allowing pointer arithmetic in user code. A more notable semantic difference is that a pointer holds a bit pattern which won't change unless code explicitly changes it; by contrast, the bit pattern required for a reference to identify a particular object may spontaneously change at any time without notice. – supercat Jan 19 '14 at 16:57
  • Considering how much confusion&damage has and is being created by "Java does not have pointers" I'm kind of sick of this discussion. And I think it is really pointless to argue, that there are no pointers, just to claim, that there is no pointer arithmetic or that there is garbage collection. As a result of this there are not a few people thinking, that in java one cannot have two pointers pointing to the same object (note the many questions regarding this). And by the way -- does "java have reference" mean, that one cannot change a "reference" to refer to a different object like in C++? – GrapschKnutsch Oct 12 '16 at 17:44