Good day.
There are two examples in which we work with a copy of an int variable. In the first example, changing the copy of an int changes the original int:
class Person{ String name; int age; Person(String s, int i){ name = s; age = i; } } class Test{ public static void main(String args[]){ Person p1 = new Person("John", 22); Person p2 = new Test().change(p1); System.out.println(p1.name + " " + p1.age); System.out.println(p2.name + " " + p2.age); } private Person change(Person p){ Person p2 = p; p2.age = 25; return p2; } } The final result:
John 25 John 25 that is, now p1.age also points to '25'. However, in the second case:
class Test{ public static void main(String args[]){ int x = 10; int y = new Test().change(x); System.out.println(x); System.out.println(у); } //В примере параметром является int x, что немного путает с толку. Может быть любой буквой. int change(int x){ x = 12; return x; } } we get the final result:
10 12 that is, x still indicates '10'. What could be the reason for this difference?
Thank you.
System.out.println(p1 == p2);- Pavel Mayorov