The value of int a remains same after calling function (testInPlaceInteger(a)) but for int[] arr changes after calling the function (squareArrayInPlace). Why is int a value not changing?
Following is my code:
public class Test {
public static void main(String[] args) {
int a = 5;
int[] arr = new int[] {3, 4};
Test test = new Test();
test.testInPlaceInteger(a);
test.squareArrayInPlace(arr);
System.out.println(a);
for (int i : arr) {
System.out.println(i);
}
}
public void testInPlaceInteger(int num) {
num *= num;
}
public void squareArrayInPlace(int[] intArray) {
for (int i = 0; i < intArray.length; i++) {
intArray[i] *= intArray[i];
}
}
}
Output:
5
9
16