reference are made equal then why false | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

reference are made equal then why false

https://www.sololearn.com/post/1327593/?ref=app

15th Oct 2021, 6:18 PM
Adithya Keshav T
Adithya Keshav T - avatar
8 Answers
+ 1
Adithya, - main() has references of a1, a2 they refer to heap where objects are - m() has local copies of that references changes of objects will remain because objects are outside m() but local copies of references will lost
16th Oct 2021, 10:05 PM
zemiak
15th Oct 2021, 6:33 PM
A͢J
A͢J - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ now i understood in stack's main frame the references of m1 & m2 doesn't change. Only the references of the parameters of m function are made equal & they are valid only till that m function call frame is on stack. Am i right
15th Oct 2021, 7:13 PM
Adithya Keshav T
Adithya Keshav T - avatar
+ 1
Adithya Keshav T Because in Java everything is passed by value so basically you are assigning one value to another value that's why there is equal but after that not because reference of a1 and a2 still didn't change.
15th Oct 2021, 6:48 PM
A͢J
A͢J - avatar
+ 1
also compare with this class A { int x; A( int px) { x=px; } } public class B { static void m(A a1, A a2) { a1.x = a2.x; } public static void main(String[] args) { A a1 = new A(1); A a2 = new A(2); m(a1, a2); System.out.println (a1.x == a2.x); // true } }
16th Oct 2021, 8:26 AM
zemiak
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ here initially in m function a1 reference & a2 reference are same but when execution comes back to main method its changes why ?? https://code.sololearn.com/c7n8GXd3QF8d/?ref=app
15th Oct 2021, 6:36 PM
Adithya Keshav T
Adithya Keshav T - avatar
0
zemiak See this https://code.sololearn.com/cUpg9XUYc4O3/?ref=app While execution of m in stack takes place the change in the values x takes in main frame so if we compare them back in main frame its comparison becomes true but its not the case when we make references equal its not happening
16th Oct 2021, 8:39 AM
Adithya Keshav T
Adithya Keshav T - avatar
0
zemiak how to get to know about this memory perspective of coding
17th Oct 2021, 8:34 AM
Adithya Keshav T
Adithya Keshav T - avatar