function - Pass references by value Java -


this question has answer here:

i have 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 print something.

i java doesn't pass objects @ all. instead, creates copy of reference passed. if understood correctly, when call modify(actual) java creates reference same object. so, have 2 references reference object actual. now, through second reference, modify object , object should change. object actual should change, because through copied reference have same access object.

can explain me fail understand concept of passing references value?

all "variables" in java references address in memory when object resides.

so have:

actual -> string("something") 

after method invocation have

actual -> string("something") 

and

m -> string("something") 

you changing m -> string("else")

bot not actual -> string("something")

where string("...") notation means object string value "...".


Comments