开发者

Can someone explain how java is pass by value only when

开发者 https://www.devze.com 2023-03-31 15:18 出处:网络
When I take an array, something like this: int anArray[] = new int[5]; //initialize to 0 doStuff(anArray);

When I take an array, something like this:

int anArray[] = new int[5];
//initialize to 0

doStuff(anArray);

//inside doStuff
anArray[3] = 731;
//passes back to main

System.out.println开发者_如何转开发(anArray[3]); // outputs 731

Isn't this pass by reference? What gives?


Reference types like arrays are still considered pass-by-value because you're passing a copy of the value of the reference. Note that you're not passing in the actual reference handle. That is to say, your doStuff method cannot alter the reference to anArray as would be possible in pass by reference semantics. An example should help understand the difference:

int anArray[] = new int[] { 99, 100};

doStuff(anArray);
...

//inside doStuff 
anArray = new int[] { 68, 69};

...
// back in anArray's original scope
System.out.println(anArray[1]); // still outputs 100, not 69


Your array is an object which lives on the heap (somewhere in memory). It exists there as long as there is at least one active reference in your program pointing to it.

You can think of a reference as just a link or pointer to your array. The reference is typically just a small value that tells your program how to get to the object you care about at its memory address. Let's say the reference points to memory address 100. When you pass the reference to a method, it is passed by value, so the method gets a copy of the reference. A new value of 100 enters the method.

When you try to do something to the array in that method, the value of the reference is considered and used to gain access to the object in memory. So, we look up address 100, get the object we care about and finish.

Once you exit your method, the value of the reference inside the method disappears (it was a local variable), but outside the method you still have a copy of the reference, and you can still use it to access address 100 where your object is.

So we just get the perception that the object is passed by reference, when instead the object is always in the same place in memory, and we are just passing a reference value which gets copied. Since the copy has the same value address as outside the method, we can gain access to the same object.


You are modifying the state of a mutable object, in this case an array. However, when you try this code out, you will find that the doStuff function gets the array passed in by value:

public class try1
{

  static void doStuff(int[] anArray)
  {
    anArray = new int[] {
        5, 6, 7
    };
    System.out.println(java.util.Arrays.toString(anArray));
  }

  public static void main(String[] args)
  {
    int[] anArray = new int[] {
        0, 1, 2
    };
    System.out.println(java.util.Arrays.toString(anArray));

    doStuff(anArray);
    System.out.println(java.util.Arrays.toString(anArray));
  }

}


Java is pass by value. Period.

What a method gets is a copy of the value of the variable content, always. This holds true both for primitives and objects. In case of objects the value of the variable is an address that tells the JVM how to find the object in the Heap Memory.

Given your case, the variable anArray at the beginning and the one inside the doStuff function both hold the same value that tells the JVM how to retrieve the same array (there is only one) in Memory. That's it.

Note that I didn't even have to use the word "reference" to give this answer... And no, I'll never use the word "pointer" when talking about Java.

A huge discussion about pass by reference vs value is here.

0

精彩评论

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