C/C++ Pointers vs Java References

Java doesn’t have pointers; Java has references.
Reference: A reference is a variable that refers to something else and can be used as an alias for that something else.
Pointer: A pointer is a variable that stores a memory address, for the purpose of acting as an alias to what is stored at that address.
So, a pointer is a reference, but a reference is not necessarily a pointer. Pointers are a particular implementation of the concept of a reference, and the term tends to be used only for languages that give you direct access to the memory address.

Let’s discuss some keypoints about pointers and references in context of C/C++ and Java:

  • C/C++ allows pointer arithmetic but Java Pointers (References) not: The term “pointer” is strongly associated with the C/C++ concept of pointers, which are variables which store memory addresses and can be modified arithmetically to point to arbitrary addresses.
    In Java, pointers only exist as an implementation detail for References. A copy of the reference is copied to the stack of a called function, pointing to the same object as the calling function and allowing you to manipulate that object. However you cannot change the object the calling function refers to.
  • Java doesn’t support pointer explicitly,  But java uses pointer implicitly: Java use pointers for manipulations of references but these pointers are not available for outside use. Any operations implicitly done by the language are actually NOT visible.
  • Pointers can do arithmetic, References can’t: Memory access via pointer arithmetic is fundamentally unsafe and for safe guarding, Java has a robust security model and disallows pointer arithmetic for this reason. Users cannot manipulate pointers no matter what may ever is the case.
  • Pointing objects: In C, we can add or subtract address of a pointer to point to things. In Java, a reference points to one thing only. You can make a variable hold a different reference, but such c manipulations to pointers are not possible.
  • References are strongly typed:  Type of a reference is much more strictly controlled in Java than the type of a pointer is in C. In C you can have an int* and cast it to a char* and just re-interpret the memory at that location. That re-interpretation doesn’t work in Java: you can only interpret the object at the other end of the reference as something that it already is (i.e. you can cast a Object reference to String reference only if the object pointed to is actually a String).
  • Manipulation of pointers can be dangerous:  On one hand, it can be good and flexible to have control over pointers by user but it may also prove to be dangerous. They may turn out to be big source of problems, because if used incorrectly they can easily break assumptions that your code is built around. And it’s pretty easy to use them incorrectly.

So overall Java doesn’t have pointers (in the C/C++ sense) because it doesn’t need them for general purpose OOP programming. Furthermore, adding pointers to Java would undermine security and robustness and make the language more complex.

CPP program to illustrate pointer manipulations

// A simple CPP program to illustrate 
// the concept of pointers and 
// their manipulations in C/C++
#include <iostream>
using namespace std;
   
int main()
{
   int number = 88;    
   int * pNumber;  
     
   // assign the address of the variable number 
   // to pointer pNumber
   pNumber = &number;  
   
   // Print content of pNumber 
   cout << pNumber << endl;  
     
   // Print address of number
   cout << &number << endl;  
     
   // Print value pointed to by pNumber
   cout << *pNumber << endl; 
     
   // Print value of number
   cout << number << endl;   
   
   // Re-assign value pointed to by pNumber
   *pNumber = 99;            
   cout << pNumber << endl;  
   cout << &number << endl;  
   cout << *pNumber << endl; 
   cout << number << endl;  
                               
   cout << &pNumber << endl; 
}

Output:



0x7fff1ae7ca94
0x7fff1ae7ca94
88
88
0x7fff1ae7ca94
0x7fff1ae7ca94
99
99
0x7fff1ae7ca98

Java Program to illustrate references

// A simple Java program 
// to illustrate the concept of 
// references 
class Rectangle 
{
  double length;
  double breadth;
}
  
class RectangleDemo
{
    public static void main(String args[])
    {
      
    // r1 is reference variable which contain 
    // the address of Actual Rectangle Object.
    Rectangle r1 = new Rectangle();
        
    // r2 is another reference variable
    // r2 is initialized with r1 means:
    // r1 and r2 both are referring same object 
    // thus it does not create duplicate object 
    // nor does it allocate extra memory.
    Rectangle r2 = r1;
      
    r1.length = 10;
    r2.length = 20;
      
    System.out.println("Value of R1's Length : " + r1.length);
    System.out.println("Value of R2's Length : " + r2.length);
  
    }
}

Output:

Value of R1's Length : 20.0
Value of R2's Length : 20.0

Related Articles:

This article is contributed by Megha Bagri. If you like Geeksf[/sourcecode]orGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.