1

I know Java methods are always pass by value, but I found this code that when you pass JSONObject into this method and modify that object inside the method, it remains modified when the method comes back.

This method is used to replace some value of json object, and it is working fine.

Can someone explain how this happens when Java is pass by value?

public static void setProperty(JSONObject js1, String keys, String valueNew) throws JSONException {
    String[] keyMain = keys.split("\\.");
    for (String keym : keyMain) {
        Iterator iterator = js1.keys();
        String key = null;
        while (iterator.hasNext()) {
            key = (String) iterator.next();
            if ((js1.optJSONArray(key) == null) && (js1.optJSONObject(key) == null)) {
                if ((key.equals(keym))) {
                    js1.put(key, valueNew);
                    return;
                }
            }
            if (js1.optJSONObject(key) != null) {
                if ((key.equals(keym))) {
                    js1 = js1.getJSONObject(key);
                    break;
                }
            }
        }
    }
    return;
}

This is how you can call it

public static void main(String[] args) throws JSONException {
        String text="{\"a\": {  \"b\": {  \"c\": \"x\",  \"d\": \"y\"  }}}";
        JSONObject json = new JSONObject(text);
        setProperty(json,"a.b.d","************");
        System.out.println(json.toString(4));

    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Derek Noble
  • 289
  • 1
  • 3
  • 13
  • 2
    You can refer to this page [Is Java “pass-by-reference” or “pass-by-value”?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Joey Chong Feb 12 '16 at 07:15
  • @joeyChong I read this and 13 other blogs article which related to this. I understand what those explained and what happen when you using new keyword. also i understand pass reference by value concept. but i cant explain this – Derek Noble Feb 12 '16 at 07:17
  • 3
    You are not changing the reference (what you can't because Java passes _by value_) but you change the internal state of the object being referred to. And this is visible in both methods because they both have a reference to the _same_ object. – Seelenvirtuose Feb 12 '16 at 07:22
  • you dont change reference but the inner "thing" what is at the reference located. – Stefan Feb 12 '16 at 07:33
  • @DerekNoble If you don't understand what's going on here, I don't think you understand those articles. References are passed by value, and the contents of the objects referred to by those references can be modified anywhere. – Louis Wasserman Feb 12 '16 at 07:40

2 Answers2

1

This method does not change the reference js1. The object itself is mutable, and the method calls js1's method to change its internal state.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

With this method:

public static void setProperty(JSONObject js1, String value) {....}

And you are calling it by

JSONObject json = ...;
setProperty(json, "xxx");

Several thing to note (In order to avoid confusion, I will use "Pointer" instead of "Reference" here):

  1. json is NOT the object itself. json is a pointer to JSONObject

  2. When you are calling setProperty(json, "xxx");, a copy of json pointer is passed into the method. Given that it is a copy of the pointer, not the object itself, that pointer is pointing to the same object instance, hence if you manipulate the content of that object, the caller will of course see the effect.

  3. You will see the effect of pass-by-value if, in your setProperty(...), you do something like js1 = null;. You will see that the value passed in by the caller is not changed.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • sorry about ping again on same thing. i have little 1 more thing to clarify. in this code js1 = js1.getJSONObject(key); its change the pointer of js1 from its original object to new object right? (new memory location) but changes do after this also effecting to original object. how we can explain this? – Derek Noble Feb 16 '16 at 16:39
  • @DerekNoble I am not sure. but I think child json objects are reside in different memory addresses. that would be the reason. BTW i am not sure about it. – Krishantha Dinesh Feb 16 '16 at 18:00
  • @derek: in brief, for `foo=foo.bar()`, it doesn't guarantee `foo` will point to new address. It may be the original value, may be totally new value, or maybe pointing to some part of the original obj. It depends on what `bar()` does – Adrian Shum Feb 17 '16 at 00:44