Java doesn’t have pointers in the same way as some other languages, but it does use references for objects. In this example, we’ve adapted the concept to show how Java handles value types (primitives) and reference types (objects).
zeroval doesn’t change the i in main, but zeroref does because it has a reference to the Integer object.
To run the program, compile it and then use java:
Note that the zeroval method doesn’t affect the original value, while zeroref does change it. The last line prints the object’s identity hash code, which is somewhat analogous to a memory address in languages with explicit pointers.
In Java, primitives are passed by value, while objects are passed by reference. The Integer class is used here to wrap the int primitive, allowing us to simulate pointer-like behavior.