Wednesday, December 19, 2018
Home > java > Java Pointers

Java Pointers

According to the concept of C/C++ pointers are variables that explicitly store memory addresses. These variables can be modified arithmetically to point to other variables or some other memory address. When we talk about java pointers the very first question that appears is whether Pointers exist in Java or not? Well, the answer to that is No! see the last part of the documentation. Sometimes you may come across the statement that pointers do exist in java. Actually, they are referring to Reference. But Reference and pointers are actually very different.

A reference is a variable which refers to some other variable or object and we can use it as an alias for that other variable or object. While a pointer is a variable which stores the memory address and acts as an alias for to the value present at that memory address. We can say that every pointer is a reference but not every reference is a pointer.

Though references can be used as pointers in java with some restrictions as the Java references provides higher-level abstraction which does not allow the programmer to manipulate the memory addresses. A java reference provides us the access to the members and member functions of the object to which it points. But not the memory location of the object.

Pointers in java (references) are very strictly typed that is we can’t change or re-interpret an object unless the object at the other end is something that it already is. I.e. you can cast the object to the string if the other object to which it points is actually the string.

Why pointers are not in java?

There are different reasons for that. But a few of those are:

  •      They provide access to the memory which is not recommended.
  •      Pointers can lead to memory leakage.
  •      The pointers are much slower than other variables.

Java Pointers Example

package example;
public class Shape {
     private int length;
     private int width;
     public int getLength() {
          return length;
     }
     public int getWidth() {
          return width;
     }
/*
*  use of this pointer or this reference. which references to the current
* object.
*/

     public void setLength(int length) {
          this.length = length;
     }
     public void setWidth(int width) {
          this.width = width;
     }
}

To make use of the above class we have the below program.

package example;
public class JavaPointers {
     public static void main(String[] args) {

          // creating an object ( a reference variable to the class).
          Shape s1 = new Shape();
          
          // using the object to set the values.
          s1.setLength(10);
          s1.setWidth(10);

          // using the object to access the values.
          System.out.println("Length is :" + s1.getLength());
          System.out.println("Width is :" + s1.getWidth());

          // creating another object ( a reference variable to the class).
          Shape s2 = new Shape();

          // using the object to set the values.
          s2.setLength(13);
          s2.setWidth(12);

          // using the object to access the values.
          System.out.println("Length is :" + s2.getLength());
          System.out.println("Width is :" + s2.getWidth());
     }
}

Output is:

java pointers example

Hence, in a nutshell, we can say that java does not have pointers or to be more precise java doesn’t allow pointers that a programmer can access or modify. This may lead you to the argument that this is a big disadvantage to not having pointers in java. But this is not true, as long as you remain in the boundary of the execution environment you won’t be needing any pointer in your programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: