Tuesday, 15 May 2012

java - Why doesn't this change the value -



java - Why doesn't this change the value -

this question has reply here:

is java “pass-by-reference” or “pass-by-value”? 58 answers public class subiectlicentatrei { public static void main(string args[]) { double d = 12.34; scadeunitate su = new scadeunitate(); su.scadeunitate(d); system.out.println(d); } } class scadeunitate { public void scadeunitate(double d) { d = d - 1.0; } }

outputs 12.34 when expect 11.34.

java pass value

so when pass here

ksu.scadeunitate(d);

d pass-by-value , when deduct here

d = d - 1.0;

d wont reference value 11.34. destroyed when out-of-scope

solution:

put homecoming value of scadeunitate method

public double scadeunitate(double d)

get homecoming value reference returned value.

d = su.scadeunitate(d);

java

No comments:

Post a Comment