Thursday, February 27, 2020

Why doesn't Java have pointers?

First we need to discuss about pointer before going further.

Pointer in c language is a variable that stores the address of other variable at run time (dynamically). It can be of any primitive data type or user defined data type. Pointer enables us to access a variable that might be defined outside the function. 

Java has pointers everywhere but terminology is different. In java, objects created are referred/pointed by reference variable. Except primitive data type, everything in java is instance or object which can be used via reference variable which is similar to pointer in c/c++. 
But there is no 'address of' (&) operator to get the address of the object as we can in c/c++.

1. we can't use pointer arithmetic like things in java. 
2. We can't use get the address of an object.
3. We can't move from one node to another by incrementing the pointer, as we can in c/c++
4. We can use pointer for an array and traverse the whole array via pointer increment or decrement.

No comments:

Post a Comment