I have the following code:
public class PassReferenceByValue {
static void modify(String m)
{
m = "Else";
}
public static void main(String [] arg)
{
String actual = "Something";
modify(actual);
System.out.println(actual);
}
}
It will print Something
.
I get that Java doesn't pass objects at all. Instead, it creates copy of the reference passed. If i understood correctly, when I call modify(actual)
Java creates another reference to the same object. So, now we have two references that reference to the object actual
. Now, through the second reference, we modify the object and the object should change. The object actual
should change, because through the copied reference we have the same access to the object.
Can somebody explain me where I fail to understand the concept of passing references by value?