I struggled coming up with a title for this post but I would love to find a simple explanation why the running the following program gives output of 6,5,5 instead of 5,5,5:
public class TestMethod {
public static void main(String[] args) {
IntegerObject aInt = new IntegerObject(5);
objectIncrease(aInt);
System.out.println(aInt.x);
int bInt = 5;
increase(bInt);
System.out.println(bInt);
Integer cInt = new Integer(5);
increase(cInt);
System.out.println(cInt);
}
public static void objectIncrease(IntegerObject x){
x.increment();
}
public static void increase(int x){
++x;
}}
IntegerObject is just another class that I created to demonstrate my point. Why does the increase(int x) method create a local instance of the int x but the objectIncrease(IntegerObject x) method does not create a local instance of the integerObject x?