In Pass By value when arguments are passed by value to a method, it means that a copy of the original variable is being sent to the method and not the original one, so any changes applied inside the method are actually affecting only the copy version and not the original one.
for example,
public class Test {
private void squareNumber(int number){
number=number*number;
}
public static void main(String[] args) {
int x=2;
System.out.println(x); //output = 2
new Test().squareNumber(x);
System.out.println(x);//output = 2
}
}
But in case of Arrays and List, this doesn't work like this and below is the example of ArrayList
public class Test {
private void squareOfList(List<Integer> integerList){
for (int i=0;i<integerList.size();i++) {
integerList.set(i,integerList.get(i)*integerList.get(i));
}
}
public static void main(String[] args) {
List<Integer> nums= new ArrayList<>();
nums.add(2);
nums.add(3);
nums.add(4);
nums.add(5);
System.out.println(nums); // output = [2, 3, 4, 5]
new Test().squareOfList(nums);
System.out.println(nums); // output = [4, 9, 16, 25]
}
}
Since we have passed nums as a parameter, only the value inside the method should change but it is changing the value of the original list. Does this "STRICTLY Pass by value" term hold good?