The following procedure illustrates the difference between value passing and application passing.
Package com.liaojianya.chapter1;/** * The demonstrates the use of the array reference. * @author LIAO Jianya * July 21, 2016 */public class Arrayreference{public static void Main (string[] args) {int x = 100;int AR R[] = {1, 2, 3, 4, 5, 6}; System.out.println ("----------before invoking Changerefervalue method-----------");p rint (x, arr); Changerefervalue ( x, arr); System.out.println ("----------after invoking Changerefervalue method-----------");p rint (x, arr);} public static void Changerefervalue (int A, int[] chgarr) {A + = 1;chgarr[0] = 0;chgarr[1] = 0;chgarr[2] = 0;} public static void Printarr (int[] arr) {for (int i:arr) {System.out.print (i + "");} System.out.println ();} public static void print (int x, int[] arr) {System.out.println ("x =" + x); System.out.print ("arr:");p Rintarr (arr);}}
Operation Result:
Analysis:
1) because the integer parameter A and the argument x is a value transfer relationship, so do not change the value of x itself, but in the Changerefervalue method, the value of x 100 is assigned to A,a + = 1, after a plus 1, the X has no effect.
2) Any modification to the array data pointed to by the parameter arr will synchronize the array data pointed to by the argument arr in the main method, which is an English-language reference where both the argument and the formal parameter point to the same block of memory space.
Reference Delivery & Value passing