Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

marked as duplicate by gnat, Bart van Ingen Schenau, Glenn Nelson, AProgrammer, Dan Pichelman Aug 5 '13 at 15:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

This depends on your definition of "pointer" and "exist"! – Jörg W Mittag Aug 5 '13 at 9:50
up vote 11 down vote accepted

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.

share|improve this answer
It is NullReferenceException in C# – Konrad Morawski Dec 15 '13 at 20:26

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++.

share|improve this answer
Those are not pointers. Pointer arithmetics and memory management are defining aspects of pointer. Without it, it is not a pointer. – Euphoric Aug 5 '13 at 6:30
@Euphoric You can do some kind of pointer arithmetic also, by using Unsafe; but as the name suggests, is it unsafe:… . – m3th0dman Aug 5 '13 at 6:50
@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 5 '13 at 8:37
@Euphoric: Yes, it very much is. C doesn't have a monopoly on defining terms. – Michael Borgwardt Aug 5 '13 at 12:01
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 5 '13 at 13:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.