6.2. Array Basics

 
[Page 179]

6.4. Passing Arrays to Methods

Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array:

   public static void   printArray(int[] array) {   for   (   int   i =     ; i < array.length; i++) { System.out.print(array[i] +   " "   ); } } 

You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3 , 1 , 2 , 6 , 4 , and 2 :

 printArray(   new int   []{   3   ,   1   ,   2   ,   6   ,   4   ,   2   }); 

Note

The preceding statement creates an array using the following syntax:

   new   dataType[]{value0, value1, ..., value  k  }; 

There is no explicit reference variable for the array. Such an array is called an anonymous array .


Java uses pass- by-value to pass arguments to a method. There are important differences between passing the values of variables of primitive data types and passing arrays.

  • For an argument of a primitive type, the argument's value is passed.

  • For an argument of an array type, the value of the argument contains a reference to an array; this reference is passed to the method.

Take the following code, for example:

   public class   Test {   public static void   main(String[] args) {   int   x =   1   ;  // x represents an int value    int   [] y =   new int   [10];  // y represents an array of int values   m(x, y)  ;  // Invoke m with arguments x and y  System.out.println(   "x is "   + x); System.out.println("y[     ] is " + y[     ]); }   public static void    m(   int   number,   int   [] numbers )  { number =   1001   ;  // Assign a new value to number  numbers[     ] =   5555   ;  // Assign a new value to numbers[0]  } } 

You will see that after m is invoked, x remains 1 , but y[0] is 5555 . This is because y and numbers reference to the same array, although y and numbers are independent variables, as illustrated in Figure 6.5. When invoking m(x,y) , the values of x and y are passed to number and numbers . Since y contains the reference value to the array, numbers now contains the same reference value to the same array.

Figure 6.5. The primitive type value in x is passed to number , and the reference value in y is passed to numbers .
(This item is displayed on page 180 in the print version)

Note

The JVM stores the array in an area of memory called the heap , which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order.



[Page 180]

6.4.1. Example: Passing Array Arguments

Listing 6.3 gives another example that shows the difference between passing a primitive data type value and an array reference variable to a method.

The example contains two methods for swapping elements in an array. The first method, named swap , fails to swap two int arguments. The second method, named swapFirst-TwoInArray , successfully swaps the first two elements in the array argument. Figure 6.6 shows a sample run of the program.

Figure 6.6. The program attempts to swap two elements using the swap method and the swapFirstTwoInArray method.
(This item is displayed on page 181 in the print version)


Listing 6.3. TestPassArray.java
(This item is displayed on pages 180 - 181 in the print version)
 1   public class   TestPassArray { 2  /** Main method */  3   public static void   main(String[] args) { 4    int   [] a = {   1   ,   2   };  5 6  // Swap elements using the swap method  7 System.out.println(   "Before invoking swap"   ); 8 System.out.println(   "array is {" + a   [     ]   + ", " + a   [   1   ]   + "}"   ); 9  swap(a[0], a[1]);  10 System.out.println(   "After invoking swap"   ); 11 System.out.println(   "array is {" + a   [     ]   + ", " + a   [   1   ]   + "}"   ); 12 13  // Swap elements using the swapFirstTwoInArray method  14 System.out.println(   "Before invoking swapFirstTwoInArray"   ); 15 System.out.println(   "array is {" + a   [     ]   + ", " + a   [   1   ]   + "}"   ); 16  swapFirstTwoInArray(a);  17 System.out.println(   "After invoking swapFirstTwoInArray"   ); 18 System.out.println(   "array is {"  + a  [     ]  +  ", "  + a  [   1   ]  +    "}"   ); 19 } 20 21  /** Swap two variables */  22   public static void    swap(   int   n1,   int   n2)  { 23   int   temp = n1; 24 n1 = n2; 25 n2 = temp; 26 } 27 28  /** Swap the first two elements in the array */  29   public static void    swapFirstTwoInArray(   int   [] array)  { 30   int   temp = array[     ]; 

[Page 181]
 31 array[     ] = array[   1   ]; 32 array[   1   ] = temp; 33 } 34 } 

As shown in Figure 6.6, the two elements are not swapped using the swap method. However, they are swapped using the swapFirstTwoInArray method. Since the parameters in the swap method are primitive type, the values of a[0] and a[1] are passed to n1 and n2 inside the method when invoking swap(a[0], a[1]) . The memory locations for n1 and n2 are independent of the ones for a[0] and a[1] . The contents of the array are not affected by this call. This is pictured in Figure 6.7.

Figure 6.7. When passing an array to a method, the reference of the array is passed to the method.

The parameter in the swapFirstTwoInArray method is an array. As shown in Figure 6.7, the reference of the array is passed to the method. Thus the variables a (outside the method) and array (inside the method) both refer to the same array in the same memory location. Therefore, swapping array[0] with array[1] inside the method swapFirstTwoInArray is the same as swapping a[0] with a[1] outside of the method.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net