开发者

java pass by value example help me to understand

开发者 https://www.devze.com 2023-01-12 11:37 出处:网络
I have the following code class sample { public void update(List l) { l = null; } pub开发者_JAVA百科lic static void main (String[] args)

I have the following code

class sample
{

  public void update(List l)
  {
      l = null;
  }

  pub开发者_JAVA百科lic static void main (String[] args)
  {

      List m = new ArrayList();
      m.add("suresh");
      m.add("sankar");
      new sample().update(m);
      System.out.println(m);
  }
}

The answer will be {["suresh,"sankar"]}. The m is a pointer to the arraylist object, it contains an memory address value (for ex consider 0xf34 ). when we pass m to update method ,the local variable l will be set to 0xf34 that points to arraylist object in memory .when we set null to this variable l , the memory address replaces the arraylist with null ,hence the variable m should also refer null.am i right.please help.


No, the compiler has it right. :)

The parameter l contains a reference to the ArrayList object assigned to m. l gets updated to null, and indeed any later use of lwithin the update() method would see it as null. But l is a separate variable that has scope only within that method -- it's not linked to m in any way (other than the fact that they originally contained references to the same object).


The key to understanding this is to remember that all objects are accessed indirectly via a reference. When an object is passed as an argument the method, the "value" actually being passed is a reference, and not the object itself.

When you null out the l parameter in the update method, you are setting that specific reference to null - the original reference m remains unchanged, and the object referred to by both references is also unchanged.

If you know C/C++, then this can be paraphrased as:

void update(List* l)
{
   l = NULL; // set the pointer to null - the object (*list) is unmodified
}

void main()
{
   List* m = ...;
   update(m);
   printf(m->values());
}

The pointer m is copied by value. The object pointed to (the list *m) is not altered in any way. The value of the pointer is copied from m to l. When l is set to NULL, that is a local change that only affects the value of the l pointer.

Here's an example where pass by reference involves a non-local change,

class NonLocalChange
{
   public void change(int[] i) {
      i[0] = 2;
      i = null;
   }

   public static void main(String[] s) {
      int[] m = new int[1];
      m[1] = 3;
      change(m);
      System.out.println(i[0]);
   }
}

The result printed is 2. This is because the change method changes the object referenced by l, and not just the reference itself.

Note that this doesn't throw a NullPointerException, even though l is assigned to null. As before, it's a reference, and so it's a local assignment to the value of that reference.


Your update method just sets the local l reference to null without changing anything about the passed-in object.


just imagine that you have a address (X for example) in your heap.

if you set m to refer to X as well as l to refer to X. we have two variable referring to same address, if you change one of them to null, the other one will remain as old value.


Lets say, new ArrayList() returns address 2000 in which the new ArrayList object is stored.

List m = new ArrayList(): lets say, m @ 9999 = 2000, here let the number following '@' indicates the address at which 'm' is stored and the number following the '=' represents the value of 'm' (that would be an address as well since it is a reference type). So now 'm' at address 9999 holds 2000 which is the address of the newly created ArrayList object.

update(m): Since Java is call-by-value always, it calls the update() method copying the value stored in m which is 2000. So now the parameter 'l' in update() definition holds the value 2000 (which is also the address of the previouly created ArrayList object). So we can say, l @ 8888 = 2000 ('l' at address 8888 holds the value 2000)

l = null: So now, l @ 8888 = null

System.out.println(m): What is the value of m now? it is still 2000. The update() method didn't change the value of m. The method has just changed its local variable 'l' value. so 'm' still refers the previously created ArrayList object and is being printed.


No, you're not right. Java, as many other high level languages, employs call by sharing.

You could think of it like that: the reference to the actual argument is passed by value. Which means that within the called function, the reference initially points to the same value, thus any changes to the value itself are visible outside the scope of the called function, but any changes to the local reference are not. It is as if a copy of the address was passed.


Some additional reading for those interested.

objects and primitives

java+pass+by+value search

0

精彩评论

暂无评论...
验证码 换一张
取 消