Are arrays passed by value or passed by reference in Java?

Possible Duplicate:
Is Java “pass-by-reference”?

Arrays are not a primitive type in Java, but they are not objects either, so are they passed by value or by reference? Does it depend on what the array contains, for example references or a primitive type?


Your question is based on a false premise.

Arrays are not a primitive type in Java, but they are not objects either ... "

In fact, all arrays in Java are objects1. Every Java array type has java.lang.Object as its supertype, and inherits the implementation of all methods in the Object API.

Like all Java objects, arrays are passed by value ... but the value is the reference to the array.

Real passing by reference involves passing the address of a variable so that the variable can be updated. This is NOT what happens when you pass an array in Java.

Here are some links that explain the difference between "pass-by-reference" and "pass-by-value":

  • http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/cplr233.htm
  • http://www.cs.fsu.edu/~myers/c++/notes/references.html
  • Related SO question:

  • Is Java "pass-by-reference" or "pass-by-value"?
  • Historical background:

    The phrase "pass-by-reference" was originally "call-by-reference", and it was used to distinguish the argument passing semantics of FORTRAN (call-by-reference) from those of ALGOL-60 (call-by-value and call-by-name).

  • In call-by-value, the argument expression is evaluated to a value, and that value is copied to the called method.

  • In call-by-reference, the argument expression is partially evaluated to an "lvalue" (ie the address of a variable or array element) that is passed to the calling method. The calling method can then directly read and update the variable / element.

  • In call-by-name, the actual argument expression is passed to the calling method (!!) which can evaluate it multiple times (!!!). This was complicated to implement, and could be used (abused) to write code that was very difficult to understand. Call-by-name was only ever used in Algol-60 (thankfully!).

  • UPDATE

    Actually, Algol-60's call-by-name is similar to passing lambda expressions as parameters. The wrinkle is that these not-exactly-lambda-expressions (they were referred to as "thunks" at the implementation level) can indirectly modify the state of variables that are in scope in the calling procedure / function. That is part of what made them so hard to understand. (See the Wikipedia page on Jensen's Device for example.)


    1. Nothing in the linked Q&A (Arrays in Java and how they are stored in memory) either states or implies that arrays are not objects.


    Everything in Java are passed-by value. . In case of Array(Which is nothing but an Object), array reference is passed by value.. (Just like an object reference is passed by value)..

    When you pass an array to other method, actually the reference to that array is copied..

  • Any changes in the content of array through that reference will affect the original array..
  • But changing the reference to point to a new array will not change the existing reference in original method..
  • See this post..

    Is Java "pass-by-reference" or "pass-by-value"?

    See this working example: -

    public static void changeContent(int[] arr) {
    
       // If we change the content of arr.
       arr[0] = 10;  // Will change the content of array in main()
    }
    
    public static void changeRef(int[] arr) {
       // If we change the reference
       arr = new int[2];  // Will not change the array in main()
       arr[0] = 15;
    }
    
    public static void main(String[] args) {
        int [] arr = new int[2];
        arr[0] = 4;
        arr[1] = 5;
    
        changeContent(arr);
    
        System.out.println(arr[0]);  // Will print 10.. 
    
        changeRef(arr);
    
        System.out.println(arr[0]);  // Will still print 10.. 
                                     // Change the reference doesn't reflect change here..
    }
    

    Arrays are in fact objects, so a reference is passed (the reference itself is passed by value, confused yet?). Quick example:

    // assuming you allocated the list
    public void addItem(Integer[] list, int item) {
        list[1] = item;
    }
    

    You will see the changes to the list from the calling code. However you can't change the reference itself, since it's passed by value:

    // assuming you allocated the list
    public void changeArray(Integer[] list) {
        list = null;
    }
    

    If you pass a non-null list, it won't be null by the time the method returns.

    链接地址: http://www.djcxy.com/p/3498.html

    上一篇: 参考,对吗?...对吗?

    下一篇: 数组是通过值传递还是通过Java中的引用传递?