java - Why doesn't pass-by-refernce work? -
this question has answer here:
i understand passed reference in java. why doesn't work in case? had thought should print out "hate" instead of "love".
class test { static class str { public string str; public void set(string str) { this.str = str; } } public static void main(string[] args) { str s = new str(); string str = "love"; s.set(str); str = "hate"; system.out.println(s.str); } }
in main
function, str
stores reference string. when doing str = "hate"
, reference changes original object "love"
has been stored in s.str
, remains there.
see this more clarification.
Comments
Post a Comment