Java Data Type Tutorial - Java Array Parameter








Array Parameter

We can pass an array as a parameter to a method or a constructor.

The type of array we pass to the method must be assignment compatible to the formal parameter type.

The following code shows how to pass an Array as a Method Parameter

public class Main {
  public static void main(String[] args) {
    int[] num = { 1, 2 };
/* w w  w .  j av a  2s . c  o  m*/
    System.out.println("Before  swap");
    System.out.println("#1: " + num[0]);
    System.out.println("#2: " + num[1]);

    swap(num);

    System.out.println("After  swap");
    System.out.println("#1: " + num[0]);
    System.out.println("#2: " + num[1]);
  }
  public static void swap(int[] source) {
    if (source != null && source.length == 2) {
      // Swap the first and the second elements
      int temp = source[0];
      source[0] = source[1];
      source[1] = temp;
    }
  }
}

The code above generates the following result.





Array Parameter Reference

Because an array is an object, a copy of its reference is passed to a method.

If the method changes the array parameter, the actual parameter is not affected.

import java.util.Arrays;
/*from   w  ww.j a  v a2s  .c om*/
public class Main {
  public static void main(String[] args) {
    int[] origNum = { 1, 2, 3 };
    System.out.println("Before method  call:" + Arrays.toString(origNum));

    // Pass the array to the method
    tryArrayChange(origNum);

    System.out.println("After method  call:" + Arrays.toString(origNum));
  }

  public static void tryArrayChange(int[] num) {
    System.out.println("Inside method-1:" + Arrays.toString(num));
    // Create and store a new int array in num
    num = new int[] { 10, 20 };

    System.out.println("Inside method?2:" + Arrays.toString(num));
  }
}

The code above generates the following result.





Elements of the Array Parameter

The values stored in the elements of an array parameter can always be changed inside a method.

The following code shows how to change elements of an Array Parameter Inside a Method

import java.util.Arrays;
/*from w  w  w  .j  av a2 s . com*/
public class Main {
  public static void main(String[] args) {
    int[] origNum = { 1, 2, 3 };
    String[] origNames = { "Java", "SQL" };
    System.out.println("Before method  call, origNum:"
        + Arrays.toString(origNum));
    System.out.println("Before method  call, origNames:"
        + Arrays.toString(origNames));

    // Call methods passing the arrays 
    tryElementChange(origNum);
    tryElementChange(origNames);

    System.out.println("After method  call, origNum:"
        + Arrays.toString(origNum));
    System.out.println("After method  call, origNames:"
        + Arrays.toString(origNames));
  }

  public static void tryElementChange(int[] num) {
    if (num != null && num.length > 0) {
      num[0] = -1;
    }
  }

  public static void tryElementChange(String[] names) {
    if (names != null && names.length > 0) {
      names[0] = "T";
    }
  }
}

The code above generates the following result.

Example

The following code shows how to change the object array element.

class Item {/*  ww  w.  ja v a2  s.  c o m*/
  private double price;
  private String name;

  public Item(String name, double initialPrice) {
    this.name = name;
    this.price = initialPrice;
  }

  public double getPrice() {
    return this.price;
  }

  public void setPrice(double newPrice) {
    this.price = newPrice;
  }

  public String toString() {
    return "[" + this.name + ", " + this.price + "]";
  }
}

public class Main {
  public static void main(String[] args) {
    Item[] myItems = { new Item("Pen", 2.11), new Item("Pencil", 0.10) };
    System.out.println("Before method  call  #1:" + myItems[0]);
    System.out.println("Before method  call  #2:" + myItems[1]);

    // Call the method passing the array of Item
    tryStateChange(myItems);

    System.out.println("After method  call  #1:" + myItems[0]);
    System.out.println("After method  call  #2:" + myItems[1]);
  }

  public static void tryStateChange(Item[] allItems) {
    if (allItems != null && allItems.length > 0) {
      allItems[0].setPrice(0.38);
    }
  }
}

The code above generates the following result.