Does Java have pointers?




No, Java does not have pointers. This was an intentional decision by the creators of Java, because most people would agree that having pointers creates a lot of potential for bugs in the code – pointers can be quite confusing, especially to new programmers. Because arrays and strings are provided as class types in Java, there is no need for pointers to those constructs. By not allowing pointers, Java provides effectively provides another level of abstraction to the programmer.

Java has references, but not pointers

But, what Java does have is references, which are different from pointers. Here are some of the differences between references in Java and pointers in C++:


1.  References store an address.  That address is the address 
in memory of the object.  So, when a class is declared like so: 
"PersonClass y = new PersonClass();", the "y" variable actually 
stores an address in memory.  If you were to look at that 
address in memory you would see the details of the 
PersonClass object.  Pointers in C++, however, point directly 
to the object.

2.  You can not perform arithmetic operations on references.  
So, adding 1 to a pointer is not possible, but is possible
 in C++.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

FOLLOW Varoon Sahgal, Author of ProgrammerInterviewon
  • Andrew Cortese

    Part 1 is incorrect. A reference stores the address of an object, and a pointer in C++ also stores the address of an object. References and pointers are the same in this sense.

  • Brian

    EVERYTHING in Java is a pointer. We just CALL them “references”, and they MOSTLY work like C++ references. But you can assign a reference in Java to be null, where you CAN’T in C++, and you can reassign a reference in Java where you CAN’T in C++.

    And you can pass a REFERENCE to an object halfway across a program in Java, and then modify the object, which is still referenced halfway across the program, and thus cause some drastically unintended consequences halfway across the program. Not that I have ever seen this happen, mind you.

    I work in both languages, and I love them both. Each has its warts, and each its strong areas. C++ does allow more ways to get yourself into trouble, but to pretend that Java’s limitations are going to prevent a programmer from screwing up is no more honest than pretending that Ada is due for a resurgence.

  • Gizachew

    so, which one is better feature for programmer?

  • shan

    okk java does not support pointer but java why thrown null pointer exception

  • Nice website i have also seen yesterday one website which have interview question http://www.tutorial4us.com/java/java-interview-question

  • the guy

    “References store an address. That address is the address in memory of the object. … Pointers in C++, however, point directly to the object.” This is inaccurate. In C++, a pointer DOES store an address, and a REFERENCE points directly to the object. So it’s only a difference of terminology. Really, the ONLY difference between C++’s pointers and Java’s so-called “references” is that you can’t do pointer arithmetic in Java. And also that “references” in Java are deallocated automatically via garbage collection.

  • gaura

    but this pointer is used in java if both local and member parameter are same in program then this pointer is used to differ them??

  • Swoop Diggity

    “You can not perform arithmetic operations on references.
    So, adding 1 to a pointer is not possible, but is possible
    in C++.”

    I think you mean “… So, adding 1 to a reference is not possible…”. Since you’ve stated Java doesnt have pointers, that line, as it is currently written, is confusing.

Your Email (required)
Your Country