I have written this code and I wanted to know why the value of the object being referenced is not being changed
All calls in java are call by value. But when the call refers to the same object , why is the object not being updated
class Main {
public static void main(String[] args) {
Integer n = 3;
triple(n);
System.out.println("Hello world! " + n);
}
public static void triple(Integer x)
{
x *= 8;
System.out.println("Hello world! " + x);
}
}
Actual output
Hello world! 24
Hello world! 3
But I thought that output should be
Hello world! 24
Hello world! 24
Is it so because of the unboxing and autoboxing for the Integer class that a new object is created with the same name as 'x', because Integer is immutable that is locally available and which does not point to the argument/ the object being passed - n.