+ 3
reference are made equal then why false
8 Antworten
+ 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
+ 2
Adithya Keshav T
No both are not equal
https://code.sololearn.com/cKdhB12bhwr9/?ref=app
+ 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
+ 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.
+ 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
}
}
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
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
0
zemiak how to get to know about this memory perspective of coding