Does Java Have Pointers

Does Java have pointers? if yes, how manipulate them?

Yes, java has pointers and they call them references.
But reference manipulation is not possible in java. That is, you can not do ref++ and things like that.
You can just allocate memory to an object and assign it to a reference, de-allocation too is done by garbage collector in JVM. So you are free of free.

Does java really have pointers or not?

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.

If Java does not supports pointers then how does it keep record of memory locations where data is saved?

Java does indeed have pointers. Let's say that you have a class A with an attribute x with setter and getter void set_x(int) and int get_x() and consider this code:

public static void foo(A a)
{
a.setx(13);
}

public static void main()
{
A a = new A();
a.setx(12);
foo(a);
System.out.println(a.getx());
}

This will print 13 so that's indeed a pointer. However, you cannot declare a pointer and make it point as freely as you can in C. Nor can you do pointer arithmetics.

And there's lots of pointers inside the Java Virtual Machine, but they are not exposed to the programmer.

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.

What is the difference between a pointer and a reference variable in Java?

A reference is sort of like a pointer that you can't do arithmetic on... although it's more opaque. While the underlying bits may be an address in virtual memory, they don't have to be. They're just a way of getting to an object (or representing the null value). So while they're not exactly the same, if you're used to thinking of a pointer as "a way of identifying an object or navigating to it" (in some sense) then yes, those thoughts apply to references too.

Java doesn't have pointers as such (unlike, say, C# which has references and pointers - the latter being used in "unsafe" code).

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



Related Topics



Leave a reply



Submit