All objects in Java are references and you can use them like pointers.

abstract class Animal
{...
}

class Lion extends Animal
{...
}

class Tiger extends Animal
{   
public Tiger() {...}
public void growl(){...}
}

Tiger first = null;
Tiger second = new Tiger();
Tiger third;

Dereferencing a null:

first.growl();  // ERROR, first is null.    
third.growl(); // ERROR, third has not been initialized.

Aliasing Problem:

third = new Tiger();
first = third;

Losing Cells:

second = third; // Possible ERROR. The old value of second is lost.    

You can make this safe by first assuring that there is no further need of the old value of second or assigning another pointer the value of second.

first = second;
second = third; //OK

Note that giving second a value in other ways (NULL, new...) is just as much a potential error and may result in losing the object that it points to.

The Java system will throw an exception (OutOfMemoryError) when you call new and the allocator cannot allocate the requested cell. This is very rare and usually results from run-away recursion.

Note that, from a language point of view, abandoning objects to the garbage collector are not errors at all. It is just something that the programmer needs to be aware of. The same variable can point to different objects at different times and old values will be reclaimed when no pointer references them. But if the logic of the program requires maintaining at least one reference to the object, It will cause an error.

Novices often make the following error.

Tiger tony = new Tiger();
tony = third; // Error, the new object allocated above is reclaimed. 

What you probably meant to say was:

Tiger tony = null;
tony = third; // OK.

Improper Casting:

Lion leo = new Lion();
Tiger tony = (Tiger)leo; // Always illegal and caught by compiler. 

Animal whatever = new Lion(); // Legal.
Tiger tony = (Tiger)whatever; // Illegal, just as in previous example.
Lion leo = (Lion)whatever; // Legal, object whatever really is a Lion.

Pointers in C:

void main() {   
    int*    x;  // Allocate the pointers x and y
    int*    y;  // (but not the pointees)

    x = malloc(sizeof(int));    // Allocate an int pointee,
                                // and set x to point to it

    *x = 42;    // Dereference x to store 42 in its pointee

    *y = 13;    // CRASH -- y does not have a pointee yet

    y = x;      // Pointer assignment sets y to point to x's pointee

    *y = 13;    // Dereference y to store 13 in its (shared) pointee
}

Pointers in Java:

class IntObj {
    public int value;
}

public class Binky() {
    public static void main(String[] args) {
        IntObj  x;  // Allocate the pointers x and y
        IntObj  y;  // (but not the IntObj pointees)

        x = new IntObj();   // Allocate an IntObj pointee
                            // and set x to point to it

        x.value = 42;   // Dereference x to store 42 in its pointee

        y.value = 13;   // CRASH -- y does not have a pointee yet

        y = x;  // Pointer assignment sets y to point to x's pointee

        y.value = 13;   // Deference y to store 13 in its (shared) pointee
    }
} 

UPDATE: as suggested in the comments one must note that C has pointer arithmetic. However, we do not have that in Java.

Answer from Sajad Bahmani on Stack Overflow
Top answer
1 of 16
287

All objects in Java are references and you can use them like pointers.

abstract class Animal
{...
}

class Lion extends Animal
{...
}

class Tiger extends Animal
{   
public Tiger() {...}
public void growl(){...}
}

Tiger first = null;
Tiger second = new Tiger();
Tiger third;

Dereferencing a null:

first.growl();  // ERROR, first is null.    
third.growl(); // ERROR, third has not been initialized.

Aliasing Problem:

third = new Tiger();
first = third;

Losing Cells:

second = third; // Possible ERROR. The old value of second is lost.    

You can make this safe by first assuring that there is no further need of the old value of second or assigning another pointer the value of second.

first = second;
second = third; //OK

Note that giving second a value in other ways (NULL, new...) is just as much a potential error and may result in losing the object that it points to.

The Java system will throw an exception (OutOfMemoryError) when you call new and the allocator cannot allocate the requested cell. This is very rare and usually results from run-away recursion.

Note that, from a language point of view, abandoning objects to the garbage collector are not errors at all. It is just something that the programmer needs to be aware of. The same variable can point to different objects at different times and old values will be reclaimed when no pointer references them. But if the logic of the program requires maintaining at least one reference to the object, It will cause an error.

Novices often make the following error.

Tiger tony = new Tiger();
tony = third; // Error, the new object allocated above is reclaimed. 

What you probably meant to say was:

Tiger tony = null;
tony = third; // OK.

Improper Casting:

Lion leo = new Lion();
Tiger tony = (Tiger)leo; // Always illegal and caught by compiler. 

Animal whatever = new Lion(); // Legal.
Tiger tony = (Tiger)whatever; // Illegal, just as in previous example.
Lion leo = (Lion)whatever; // Legal, object whatever really is a Lion.

Pointers in C:

void main() {   
    int*    x;  // Allocate the pointers x and y
    int*    y;  // (but not the pointees)

    x = malloc(sizeof(int));    // Allocate an int pointee,
                                // and set x to point to it

    *x = 42;    // Dereference x to store 42 in its pointee

    *y = 13;    // CRASH -- y does not have a pointee yet

    y = x;      // Pointer assignment sets y to point to x's pointee

    *y = 13;    // Dereference y to store 13 in its (shared) pointee
}

Pointers in Java:

class IntObj {
    public int value;
}

public class Binky() {
    public static void main(String[] args) {
        IntObj  x;  // Allocate the pointers x and y
        IntObj  y;  // (but not the IntObj pointees)

        x = new IntObj();   // Allocate an IntObj pointee
                            // and set x to point to it

        x.value = 42;   // Dereference x to store 42 in its pointee

        y.value = 13;   // CRASH -- y does not have a pointee yet

        y = x;  // Pointer assignment sets y to point to x's pointee

        y.value = 13;   // Deference y to store 13 in its (shared) pointee
    }
} 

UPDATE: as suggested in the comments one must note that C has pointer arithmetic. However, we do not have that in Java.

2 of 16
71

As Java has no pointer data types, it is impossible to use pointers in Java. Even the few experts will not be able to use pointers in java.

See also the last point in: The Java Language Environment

🌐
Upgrad
upgrad.com › home › blog › software development › pointers in java explained: learn how to use them effectively!
Using Pointers in Java: The Ultimate Guide You Can't Miss!
July 6, 2026 - In Java, pointers are abstracted as references and are used internally, especially when working with complex data types like arrays or objects. Unlike C++, Java developers don’t need to manage or manipulate pointers directly—this is handled by the language, ensuring safety and reducing the chances of bugs.
Discussions

Do pointers in Java exist?

Sort of? There is an omnipresent thing called NullPointerException so... I'd say that's our clue that yes.

What there isn't, is pointer arithmetic. You can't "add an index" to a pointer in Java. Use arrays for that. Generally you can't point to an arbitrary zone of memory, the language doesn't offer a way to express that... And neither does the JVM platform.

You also can't choose whether you'll be using a pointer, by opposition to keeping things locally. Primitive types are kept locally (stack, or a field of the class that declared them). Objects are pointed to. That's it. You don't choose. If you have an object you know it's pointed to. If you have a primitive you know it isn't.

Sounds complicated to remove the options offered by other languages, but in truth it makes things way simpler.

More on reddit.com
🌐 r/javahelp
8
8
December 31, 2017
In Java, are all objects also pointers?
The object itself is not a pointer, but all variables (or fields) that have "object types" really store pointers, not the object itself. (The Java documentation calls this a "reference" rather than a "pointer", but it's essentially the same thing.) More on reddit.com
🌐 r/learnprogramming
3
1
October 31, 2022
Do Java primitives not use pointers?
However, is it not the same case for primitive types? When I declare int x = 5 does it not store the memory address for variable "x" that points to the 4 bytes that store the integer 5? No, primitive values are not pointers. When you say x = 5, you're storing the bytes representing the number 5 into the memory allocated for x. When you say y = x, likewise, the right hand side is evaluated to produce the integer 5, and the bytes representing that integer are stored in the variable y. So both variables have copies of the same integer value. Thus, in the following line when you set x = 2, shouldn't that affect the "y" variable as well? No. In fact, assigning a new value to the variable x will never change the contents of the variable y, regardless of whether the variables contain primitive values or object pointers. A variable always stores a value. That value might be a pointer, or an object reference. Its possible for two variables to contain references to the same object, so if you modify the object through one reference, the changes will be visible through the other reference. But in Java, a variable can't point to another variable. Changing the value of a primitive variable to a different primitive, or the value of a reference variable to a different reference, doesn't affect any other variables. More on reddit.com
🌐 r/learnprogramming
9
2
January 15, 2023
I am absolutely confused on the topic of references vs pointers
The main problem is that even in the language, a reference is the object, except it only behaves like that in some contexts, in other contexts, it behaves like a pointer. So, kinda, both statements are wrong and correct at the same time. Personally, I do consider the "a reference is a pointer with syntactic sugar" logic more useful. More on reddit.com
🌐 r/cpp
116
100
November 22, 2023
People also ask

Do references in Java affect performance compared to pointers in C++?
Java's reference system introduces some performance overhead due to automatic memory management and garbage collection. However, this trade-off improves code reliability and developer productivity by eliminating common pointer errors. In contrast, C++ pointers offer more control and performance but at the cost of higher complexity and error potential. While Java references eliminate pointer-related errors, the added cost of garbage collection can hinder performance in highly optimized, low-level applications.
🌐
upgrad.com
upgrad.com › home › blog › software development › pointers in java explained: learn how to use them effectively!
Using Pointers in Java: The Ultimate Guide You Can't Miss!
Can references in Java point to primitive data types?
No, references in Java can only point to objects. For primitive data types, Java uses variables directly. However, Java provides wrapper classes (such as Integer, Double, etc.) that allow primitive types to be treated as objects when necessary. This approach ensures the safety and consistency of the reference system in Java. Wrapper classes like Integer and Double allow autoboxing and unboxing, ensuring seamless conversion between primitive types and objects.
🌐
upgrad.com
upgrad.com › home › blog › software development › pointers in java explained: learn how to use them effectively!
Using Pointers in Java: The Ultimate Guide You Can't Miss!
How does Java handle reference types in a multithreaded environment?
In Java, reference types are inherently thread-safe when accessed in isolation, but can become problematic in concurrent scenarios. Synchronization must be used to avoid race conditions when multiple threads modify the same reference. Java provides atomic reference classes like AtomicReference to handle reference updates safely in multithreaded environments. This ensures consistency while avoiding the overhead of locking, allowing for more efficient concurrent access to reference types.
🌐
upgrad.com
upgrad.com › home › blog › software development › pointers in java explained: learn how to use them effectively!
Using Pointers in Java: The Ultimate Guide You Can't Miss!
🌐
HWS
math.hws.edu › eck › cs124 › javanotes3 › java2cpp › s2.html
Java Programing: Appendix 1, Section 2
Some variables can store pointers, but only variables that are of special · pointer types. When you use a pointer variable, the computer does not follow the pointer automatically -- if you want the computer to follow the pointer, you have to use a special notation to tell it to do so. You can create a new object for a pointer variable to point to, using a "new" operator that is similar to the new operator in Java.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java Pointers (References) Example - Java Code Geeks
August 10, 2021 - Check out our detailed example about the Java Pointers (References)! Java pointer has four types of references which are strong, weak, soft and phantom references.
🌐
GeeksforGeeks
geeksforgeeks.org › is-there-any-concept-of-pointers-in-java
C/C++ Pointers vs Java References | GeeksforGeeks
May 8, 2017 - 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 ...
🌐
Reddit
reddit.com › r/javahelp › do pointers in java exist?
r/javahelp on Reddit: Do pointers in Java exist?
December 31, 2017 -

I was wondering if there are pointers in Java. I know that Java works with references but are they similar to or the same with C++ pointers? What are their differences?

Find elsewhere
🌐
Java Mex
javamex.com › java_equivalents › pointers.shtml
The Java equivalent of pointers
When we need to refer to a "buffer of data" in Java, we could commonly use either a byte array or an instance of ByteBuffer. Prior to C++, one use of void *ptr to effectively perform object-oriented programming in C: we could pass a pointer that would get case to the appropriate struct type ...
🌐
TutorialsPoint
tutorialspoint.com › What-is-the-difference-between-Java-references-and-pointers-in-other-languages
What is the difference between Java references and pointers in other languages?
July 30, 2019 - Once we create a variable of these types (i.e. when we create an array or object, class or interface). These variables only store the address of these values. Default value of any reference variable is null. A reference variable can be used to refer any object of the declared type or any compatible type. ... A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.
🌐
Javatpoint
javatpoint.com › c-pointers
C Pointers - javatpoint
July 8, 2016 - C dereference pointer As we already know that "what is a pointer", a pointer is a variable that stores the address of another variable. The dereference operator is also known as an indirection operator, which is represented by (*). When indirection operator (*) is used with the... ... We are excited to announce that we are moving from JavaTpoint.com to TpointTech.com on 10th Feb 2025.
🌐
Medium
somesh4553.medium.com › pointers-in-java-7f4d46a6a571
Pointers In Java. A confusing topic got demystified… | by Somesh | Medium
June 25, 2023 - But but but, we use pointers a lot in java not explicitly but implicitly. Lets get it demystified. ... When we pass primitive types to a method, then it is pass by value. Which means an exact copy of the value is passed and altering it inside that method won’t change the exact value.
🌐
DZone
dzone.com › coding › languages › pointers in java
Pointers in Java
January 19, 2016 - Of course the function also has ... with a pointer to a whatever type s has. When you read PASCAL code you can not tell at the place of the actual function call if the argument is passed-by-value and thus may be modified by the function. In case of C you have to code it at both of the places and whenever you see that the argument value &s is passed you can be sure that the function is capable modifying the value of s. What is it then with Java...
🌐
TutorialsPoint
tutorialspoint.com › C-Cplusplus-Pointers-vs-Java-references
C/C++ Pointers vs Java references\\n
Value of x: 5 Address of x: 0x7ffd8e2101d4 Value stored in pointer ptr: 5 New value of x after modification: 10 · In Java, references are used to access objects stored in memory. Unlike pointers in C/C++, they don't show memory addresses or allow pointer arithmetic. All non-primitive types, such ...
🌐
Medium
medium.com › javarevisited › pointers-in-java-a36e626754b2
Pointers in Java. How to Handle Objects Efficiently | by Auriga Aristo | Javarevisited | Medium
October 18, 2024 - int a = 10; int *p = &a; // p is a pointer to the address of variable 'a' std::cout << *p; // Outputs 10 (value at the address) public static void main(String[] args) { int number = 10; Integer numberObject = number; // `numberObject` is a… ... A humble place to learn Java and Programming better.
🌐
Quora
quora.com › Do-pointers-exist-in-Java
Do pointers exist in Java? - Quora
Answer (1 of 17): Java has primitive types and reference types. A reference is a pointer to an object. This is specifically stated in the Java Language Specification section 4.3.1: > The reference values (often just references) are pointers to these objects, and a special null reference, which r...
🌐
Oracle
docs.oracle.com › cd › E19253-01 › 817-6223 › chp-pointers › index.html
Chapter 5 Pointers and Arrays
For example, the following two D code fragments are equivalent in meaning: ... The left-hand fragment creates a D global variable pointer p. Because the kmem_flags object is of type int, the type of the result of &`kmem_flags is int * (that is, pointer to int).
🌐
Quora
quora.com › What-is-a-pointer-Does-Java-support-pointers
What is a pointer? Does Java support pointers? - Quora
Answer (1 of 6): Pointer is just a variable holds address of another variable of same type: eg: int a; int ptr=&a; here ptr is pointer holding address of a. No,Java does not support pointer due to security reason, because if you gets address of any variable you could access it any where from...
🌐
Wikipedia
en.wikipedia.org › wiki › Pointer_(computer_programming)
Pointer (computer programming) - Wikipedia
1 month ago - Many languages, including most functional programming languages and recent imperative programming languages like Java, replace pointers with a more opaque type of reference, typically referred to as simply a reference, which can only be used to refer to objects and not manipulated as numbers, ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Pointers in Java - Java Code Geeks
January 7, 2016 - Of course the function also has ... with a pointer to a whatever type s has. When you read PASCAL code you can not tell at the place of the actual function call if the argument is passed-by-value and thus may be modified by the function. In case of C you have to code it at both of the places and whenever you see that the argument value &s is passed you can be sure that the function is capable modifying the value of s. What is it then with Java...
🌐
Intellipaat
intellipaat.com › home › blog › how to use pointers in java? benefits and working
How to Use Pointers in Java? Benefits and Working - Intellipaat
August 13, 2026 - Pointers in Java are not directly accessible in the language, but the Java Virtual Machine (JVM) utilizes them implicitly for managing object references. These references are variables that store the memory address of objects.