How to Use Pointers in Java

How Java pointers work?

In Java, instead of pointers you have references to objects. You cannot pass a primitive type by reference, but you can wrap a primitive type inside an object and then pass a reference to that object.

Java provides the type Integer which wraps int, however this type is immutable so you cannot change its value after construction. You could however use MutableInt from Apache Commons:

void change(MutableInt x) {
x.increment();
}

The change to x will be visible to the caller.


Specifically, I want to point to a Vector object

When you write Vector v = ...; you are assigning a reference to a vector to the variable v. A reference in Java is very similar to a pointer. References are in fact implemented internally using pointers.

Java uses pass by value. When you pass a vector to a method, you are actually copying a reference to that vector. It does not clone the vector itself. So passing a reference in Java is very similar to passing a pointer in C++.

Pointer to pointer equivalent in java

The short answer is yes. But first, a lesson in Java...

In Java, whenever you use objects, pointers are involved. If you have an object, the variable that "holds" that object is actually a pointer to that object. So if you are working with objects, you are already using pointers.

Now for primitive data types (e.g., integers, chars, or floating point numbers), Java does not use pointers, though. So if you want pointers for primitive data types, you need to use a wrapper class, such as Integer, which effectively promotes the value to an object.

Note, however, that the default wrapper classes are immutable.

If you want double-pointers (a pointer to a pointer) or triple pointers, you will need to create custom wrapper classes, like an ObjectWrapper class, that allows you to set up an arbitrary number of objects each pointing to (or "holding") the next.

Pointers are replaced with what in Java?

There are no pointers in java. Java works with references.

There is no concept of dynamic memory allocation in java. And hence there is no alternative of malloc/calloc in java. The JVM takes care of creating and releasing the memory for objects. As JVM has built in functionality of garbage collection hence no alternative to free is also provided.

Pointers in Java

The terminology is quite fuzzy here.

Java supports what it calls "references". References act a lot like pointers in C/C++-like languages. They don't act the same way "references" work in those languages.

The major differences between a pointer in C and a reference in Java are:

  • You can't do pointer arithmetic in Java (i.e. you can't "add" or "subtract" from a Java reference, you can only dereferencere it or compare it with another one).
  • You can't cast it to an incompatible type: Java is strongly type-safe, you can't "re-interpret" the bytes in memory as some other object.

For some uses of pointers this has no real effect (for example linked lists work pretty much the same in both languages), for others the difference is quite major (arrays in C are just fancy pointer arithmetic, in Java they work quite differently).

So in a way Java references could be called "restricted pointers".

Wikipedia defines a pointer as

... a programming language data type whose value refers directly to (or "points to") another value

Emphasis mine. According to this strict definition, Java doesn't have pointers.

The more general reference is the superclass of pointers, but also contrains more abstract things like file handles or even URLs.



Related Topics



Leave a reply



Submit