Pointers in Squirrel
Java doesn’t have direct pointer manipulation like some other languages. Instead, it uses references for objects. However, we can demonstrate similar concepts using object references and primitive wrapper classes.
In this Java example, we demonstrate concepts similar to pointers using object references and a custom mutable class.
The zeroRef
method takes an Integer
object. In Java, Integer
is immutable, so assigning a new value to the parameter doesn’t affect the original variable in the calling method.
The zeroMutable
method takes a MutableInt
object, which we’ve created to simulate pointer-like behavior. Changes to this object’s value
field are reflected in the calling method.
In the main
method:
- We create an
Integer
object and print its initial value. - We call
zeroRef
, which doesn’t change the originali
variable. - We create a
MutableInt
object and callzeroMutable
, which does change the object’s value. - Finally, we print the hash code of the
Integer
object, which serves as a unique identifier (similar to a memory address in other languages).
When you run this program, you’ll see output like this:
This demonstrates that zeroRef
doesn’t change the original value, but zeroMutable
does, similar to how pointers work in other languages. The hash code at the end is a unique identifier for the object, analogous to a memory address.
Remember, Java manages memory automatically and doesn’t provide direct access to memory addresses like some other languages do. This example aims to demonstrate similar concepts within Java’s object-oriented paradigm.