Consider the code:
static void incr(Integer n)
{
n++;
}
public static void main(String args[])
{
Integer n = 66; // Autoboxing
incr(n);
System.out.println(n);
}
The above program outputs 66. I expected it to print 67 and here is why. Boxing a value converts the primitive value to an object, right. In case of objects, Java passes the pointer to the object to a called function, that's incr in the above example. incr() increments the value in the memory cell passed to it. So, the change in value must occur in n too. Now please tell me, what am I missing?