Why doesn't Java have pointers?

Quora Feeds

Active Member
Xuan Luo

Java does have things that are pointers. But the terminology is different and you cannot use them freely like in C.

In Java, types are divided into primitive types (int, long, short, char, byte, double, float, boolean) and reference types (everything else). Object, String, Object[], etc. are all reference types. A value of a reference type is called a reference. A reference is a pointer to an object, unless it is the null reference. (See JLS 4.3.1 http://java.sun.com/docs/books/j...)

Note that in Java, "objects" are not values themselves -- they must always be manipulated through references; when you create an object, you get a reference; when you access a field or call a method, you do it through the reference. When you assign one reference to another, it copies the reference, so you have multiple pointers to the same object.

Objects are always behind exactly one level of indirection -- you can't have the object itself as a value (zero levels of indirection), nor can you have a pointer to pointer (higher levels of indirection) -- so there is no mechanism to "take the address of something" or "dereference a pointer" like you do in C. Furthermore, primitives are not pointers and there are no pointers to primitives.

C has features like "pointer arithmetic", the ability to convert between pointers and integers, and comparison of pointers. None of these are essential to the concept of a "pointer", and Java references have none of these, other than comparing the equality of references using ==.

See Questions On Quora

Continue reading...
 
Top