0

There is an example program in my java book that makes no sense to me. Basically it passes an array reference to a method. But the outcome is that the array itself is modified even though the method doesn't have a return or something within it that indicates its doing something other than creating it's own instance of the array.

public class PassArray 
{
   public static void main( String[] args )
   {
      int[] array = { 1, 2, 3, 4, 5 };

      System.out.println( 
         "Effects of passing reference to entire array:\n" +
         "The values of the original array are:" );

      for ( int value : array )
         System.out.printf( "   %d", value );

      modifyArray( array ); // pass array reference to method modifyArray
      System.out.println( "\n\nThe values of the modified array are:" );

      // output the value of array (why did it change?)
      for ( int value : array )
         System.out.printf( "   %d", value );

   } // end main

   // multiply each element of an array by 2 
   public static void modifyArray( int array2[] ) // so this accepts an integer array as an arguement and assigns it to local array array[2]
   {
      for ( int counter = 0; counter < array2.length; counter++ )
         array2[ counter ] *= 2;
   } // What hapened here? We just made changes to array2[] but somehow those changes got applied to array[]??? how did that happen?  

   //What if I wanted to not make any changes to array, how would implement this code so that the output to screen would be the same but the value in array would not change?

} // end class PassArray

Please explain why this is the case and also how this could be implemented somehow so that the values of array are not changed. S

AbdullahC
  • 6,649
  • 3
  • 27
  • 43
sputn1ck
  • 163
  • 1
  • 2
  • 11

4 Answers4

2

// What hapened here? We just made changes to array2[] but somehow those changes got applied to array[]??? how did that happen?

Because java is pass by reference value. Copy of the reference will be passed to the method. This reference also still points to original array. Any change you perform on this reference will reflect on original array.

how this could be implemented somehow so that the values of array are not changed.

One way is, create new array inside the method and assign it this reference.

Example:

public static void modifyArray( int array2[] ) 
   {
      array2 = new int[10];
      //Copy only ten elements from outer array, which populates element at index 2.
      for ( int counter = 0; counter < array2.length; counter++ )
      array2[ counter ] *= 2;
   } 

Now the updates/operations you perform on this reference will effect on new array created inside the method, not on original array.

See this SO discussion for more information.

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
  • @BrianRoach: I don't think my example changes original array content, which is what OP expecting. Am I missing something? – kosa Dec 12 '12 at 05:35
  • Nevermind, I'm seeing things. :) Well, no ... so, there'd be nothing in the array to `*=` ... you'd need to copy the contents first – Brian Roach Dec 12 '12 at 05:36
  • @BrianRoach: I see what you mean. Added comment over there. – kosa Dec 12 '12 at 05:39
1

When you pass an array to a method, you're really just passing a copy of the reference to the array.

Any changes that are made to the array will be reflected in the object that was passed.

What actually gets passed to modifyArray is a copy of the array reference, which is why people say Java is "pass by reference value".

jahroy
  • 22,322
  • 9
  • 59
  • 108
1

What's happening is that when you pass array to modifyArray(int[]) is that array which is actually not a java primitive, so array2 is really still the same object as array.

A fairly easy way to copy the first array would be to use System's arrayCopy method like this

  public static int[] modifyArray( int array[] )
  {
      int length = array.length; // length of the original array
      int array2[] = new int[length]; // the new array which we will copy the data into
      System.arraycopy(array, 0, array2, 0, length); // now we copy the data from array[] into array2[]
      for ( int counter = 0; counter < array2.length; counter++ ) {
          array2[ counter ] *= 2; // multiply by 2
      }

      return array2; // return the array with the new values
  }

You now have a copy of the original array, but with all the values multiplied by 2.

I hope this helps.

chossenger
  • 613
  • 6
  • 15
0
  public class PassArray 
     {
      public static void main( String[] args )
        {
      int[] array = { 1, 2, 3, 4, 5 };

      System.out.println( 
         "Effects of passing reference to entire array:\n" +
         "The values of the original array are:" );

      for ( int value : array )
         System.out.printf( "   %d", value );

      modifyArray( (int[])array.clone(); ); // pass array reference to method modifyArray
      System.out.println( "\n\nThe values of the modified array are:" );

      // output the value of array (why did it change?)
      for ( int value : array )
         System.out.printf( "   %d", value );

   } // end main

   // multiply each element of an array by 2 
   public static void modifyArray( int array2[] ) // so this accepts an integer array as an arguement and assigns it to local array array[2]
   {
      for ( int counter = 0; counter < array2.length; counter++ )
         array2[ counter ] *= 2;
   } // What hapened here? We just made changes to array2[] but somehow those changes got applied to array[]??? how did that happen?  

   //What if I wanted to not make any changes to array, how would implement this code so that the output to screen would be the same but the value in array would not change?

   }

Please read this link to understand better

yednamus
  • 582
  • 1
  • 4
  • 22