java - What does the following program? -
this question has answer here:
class ideone { int x; public static void main (string[] args) throws java.lang.exception { ideone i; = new ideone(); i.x = 25; system.out.println(i.x); f(i); system.out.println(i.x); g(i); system.out.println(i.x); } public static void f(ideone j){ j = new ideone(); j.x = 55; // changes new instance of j. not changing actual object } public static void j(ideone j){ j.x = 52; // modifies actual object } }
i have doubt in this. in case of j function, modifies actual object. not pass reference? passing object , modifying inside j.
but doing samein function f also. passing object , modifying it. not modifying original object.
i confused behaviour between these.
as per highly voted answer, passing url in both cases. , modifying content in url. visible in 1 case not in other case.
the f
method overwrites reference j
passed it. therefore cannot change state of ideone
instance reference passed caller. note assignment j = new ideone()
not affect caller of f
, since java passes value of reference variable, not reference it.
the j
method doesn't overwrite reference, can modify state of instance referred it.
Comments
Post a Comment