CODE SNIPPET:
class ReferenceVariable {
int num;
}
public class ExampleFourteen {
public static void main(String[] args) {
int i = 10;
int j = 5;
System.out.println(i + " " + j);
i = j;
System.out.println(i + " " + j);
j++;
System.out.println(i + " " + j);
ReferenceVariable numOne = new ReferenceVariable();
ReferenceVariable numTwo = new ReferenceVariable();
System.out.println(numOne.num + " " + numTwo.num);
numOne = numTwo;
numOne.num = 10;
System.out.println(numOne.num + " " + numTwo.num);
numTwo.num = 100;
System.out.println(numOne.num + " " + numTwo.num);
numTwo.num++;
System.out.println(numOne.num + " " + numTwo.num);
}
}
Hi! I am not able to understand the difference between call by reference and call by value.
My understanding is that numOne and NumTwo are two separate objects. Both have instance variables num (from class ReferenceVariable) separate from each other.
I would really appreciate if you could help me explain it. Thanks!