To pass an array to a method in Java, you must use the square bracket ( [ ) operator ( but not the square braces ( ) operator) to create a temporary array in which to pass the array contents. This temporary array does not persist after the method returns. This is different than the array you declare in your code that persists in memory.
In the past several years, I’ve been working on a few projects that required passing array parameters to a method. This is a fairly common pattern, and a performance optimization. In this article, I’ll show you how to optimize this pattern for better performance.
Java supports passing arrays to methods by using the “array” keyword, e.g.: public class Person { private String name; private int age; private int height; public int getAge() { return age; } public void setAge(int age) { this.age = age; } } && public class Pair { private int m1; private int m2; public Pair(int m1, int m2) { this.m1 = m1; this.m2 = m2; } public int computeDifference(Pair pair) { return m1 – m2; } }
Passing arrays to Java methods | Just as we can provide basic type values and objects to methods, we can also pass arrays to them.
Arrays and individual array items may also be sent as parameters to functions, and methods can return arrays.
In Java, when we provide an array parameter to a method, we are really passing the array’s reference to the function, not its value.
When an array is provided as a parameter to a method, the method’s statements may access all of the array’s elements.
With the help of sample programs, we will learn how to provide one-dimensional and multidimensional arrays as parameters to methods in Java. So, let’s have a look at them one by one.
In Java, passing a one-dimensional array as an argument to a method
The parameter list of a method that will accept an array reference through a method call must specify an array argument. The following is the typical syntax for declaring a method with a one-dimensional array argument list:
data-type array name(data-type array name[ ]) data-type method-name(data-type array name[ ]) data-type method-name( / adverbial adverbial adverbial For instance, int num[ ]) void display(int num[ ]) void display(int num[ ]) void display(int num[ ]) void
The array name is preceded by a pair of square brackets, indicating that we are sending an array.
After defining a method, we must provide the array name of the real array as a parameter to the method when calling it. To send an array as a parameter to a method, leave off the brackets and just type the name of the array.
The following is the typical syntax for calling a method using an array reference:
method-name(array name);
For instance, suppose the array num is defined as follows:
num = new int[4]; int[ ] num = new int[4]; int[ ] num = new int[
then the m1() method is called
m1(num);
Here, we’re providing the array num’s reference to the m1 function (). We don’t need to provide array length as an extra parameter when passing the reference of an array object to a function.
While calling a method, we may also provide an anonymous array as a parameter. The following is the typical syntax for invoking a method using an anonymous array as an argument:
method-name(new data-type[ ]value0, value1,…, valuek); For example: m1(new int[ ]3, 1, 2, 6, 4, 2);
Let’s look at a basic program that displays items from a one-dimensional array.
1st source code for the program:
package arraysProgram; public class OneDArray { void show(int x[ ]) { for(int i = 0; i < x.length; i++) { System.out.print(x[i]+ ” “); } } } public class OneDArraytoMethod { public static void main(String[ ] args) { // Declare an array x with initialization. int[ ] x = {2, 3, 4, 5, 6, 7, 8}; // Create an object of class OneDArray. OneDArray obj = new OneDArray(); System.out.println(“Value of array x: “); obj.show(x); // Calling show() method with passing 1D array. } } Output: Value of array x: 2 3 4 5 6 7 8
We’ve created a class OneDArray with a method called display in this application (). The function display() takes a one-dimensional array as a parameter but returns nothing. The display() function prints the items of a one-dimensional array that is given to it.
The main method creates a one-dimensional array x with element initialization. obj.show(x); invokes the show() function with the 1D array x as a parameter, and displays the array’s elements on the console.
In Java, arrays are provided by reference rather than by value.
We already know that pass-by-value (also known as call-by-value) is how Java passes parameters to methods. However, there is a significant difference between sending the values of basic data type variables and passing arrays. They are as follows:
1. When a primitive type parameter (boolean, char, byte, short, int, long, float, or double) is given to a method, the value of the argument is passed as well. It implies that changing a value given to a method has no effect on the original value.
For additional information, see this Java tutorial: Call by Value versus Call by Reference.
2. Arrays are objects in Java. The values are saved at a memory location held by an object. When an array type parameter is given to a method, its value is really a reference to an array that includes a copy of the memory location where the values are stored.
As a result, complete arrays are provided by reference rather than value. Simply said, if we modify an array given to a method, the original array is impacted as well.
Let’s look at an example program to assist us grasp this idea. Concentrate on the source code to get a deeper understanding.
Source code for the second program:
arraysProgram package; public class PassingArrays public static void main(String[] args) int x = 2; / Original value int[ ] num = 0, 1; / Original array int[ ] num = 0; / Original array int[ ] num = 0; / Original array int[ ] num = 0; / Original array int[ ] num = 0; / Original array int[ ] num m1(num, x); System.out.println(“Value of x: ” +x); System.out.println(“Value of num[1]: ” +num[1]); public static void m1(int x, int[ ] num) x = 5; / Changing the value of x. num[1] = 20; / Changing the value of the array. Value of x: 2 as an output 20 is the value of num[1].
As you can see in the output, the value of x remains the same, but the array values change when the method is updated.
As a result, if we modify the array within the function, the change will be visible outside the method as well. To learn more, look at the diagram below.

Note:
a) Arrays are objects in Java. The heap memory, which is utilized for dynamic memory allocation, holds objects in the JVM. To put it another way, arrays are kept in heap memory.
b) An array is not passed by reference, but a reference to an array is supplied by value, according to certain Java publications.
c) When a method receives an individual array element as a parameter, the called method receives a copy of the element’s value.
It implies that Java passes an individual array element as a parameter to a method using pass by value. As a result, if the value of an individual array element given to a method is changed, the original array element remains unaffected.
Let’s look at a program that uses this idea as an example. Concentrate on the source code below to get a better understanding.
Source code for the third program:
m1(num[0], num[1]); / Here, passing individual array items. package arraysProgram; public class PassingArrays; public static void main(String[] args) int[ ] num = 2, 4; / Original array (by pass by value mechanism). System.out.println(“Value of num[0]: ” +num[0]); System.out.println(“Value of num[1]: ” +num[1]); public static void m1; System.out.println(“Value of num[0]: ” +num[0]); System.out.println(“Value of num[1]: ” +num[1]); (int x, int y) x = 5; y = 20; x = 5; y = 20; x = 5; y = 20; x = 5; y = 20; x = 5; y = 20; x = 5; The value of num[0] is 2 in the output. 4 is the value of num[1].
4. Write a program to show the difference between sending a complete array to a method and passing an array member of a basic data type to a method.
Source code for the fourth program:
package arraysProgram; public class PassingArrays { public static void modifyArray(int[ ] num) { for(int i = 0; i < num.length; i++) num[i] *= 2; } public static void modifyingArrayElement(int arrayElement) { arrayElement *= 5; System.out.println(“nValue of arrayElement after modifying: ” +arrayElement); } public static void main(String[] args) { int[ ] num = {10, 20, 30, 40, 50}; System.out.println(“Effect of passing reference to entire array:”); System.out.println(“Original array: “); // Displaying elements of original array. for(int values : num) System.out.printf(” %d”, values); modifyArray(num); // Passing array reference. System.out.println(“nModified array: “); // Displaying modified array elements. for(int values : num) { System.out.printf(” %d”, values); } System.out.println(“nnEffect of passing an element of array:”); System.out.printf(“num[3] before modifying: ” +num[3]); modifyingArrayElement(num[3]); // Attempt to modify num[3]. System.out.println(“num[3] after modifying: ” +num[3]); } } Output: Effect of passing reference to entire array: Original array: 10 20 30 40 50 Modified array: 20 40 60 80 100 Effect of passing an element of array: num[3] before modifying: 80 Value of arrayElement after modifying: 400 num[3] after modifying: 80
It’s important to remember that changing the value of an individual primitive type array element within the called method has no effect on the original value of that element in the calling method’s array.
In Java, passing two-dimensional arrays to methods
To provide a two-dimensional array as an argument to a method, we must first give the two-dimensional array name, followed by two pairs of square brackets, and then the array data type.
The following is the general syntax for method definition:
/ local variables / statements data-type method-name(data-type array[ ][ ]) void show(int x[ ][ ]) / local variables / statements void show(int x[ ][ ]) void show(int x[ ][ ]) void show(int x[ ]
Simply provide the array name to the method when calling a method with a two-dimensional array argument list:
object-reference.show(x);
x is a two-dimensional array stated in the program, and object-reference is a reference to an object of the underlying class.
5. Let’s look at an example program that displays matrix elements.
Source code for the fifth program:
package arraysProgram; public class TwoDArray { void show(int x[ ][ ]) { int i, j; System.out.println(“Matrix x: “); for(i = 0; i < x.length; i++) { for(j = 0; j < x.length; j++) System.out.print(x[i][j]+ ” “); System.out.println(); } } } public class PassingTwoDArrayToMethod { public static void main(String[] args) { int x[ ][ ] = {{1, 2},{3, 4}}; TwoDArray obj = new TwoDArray(); obj.show(x); } } Output: Matrix x: 1 2 3 4
The display() function in this software is intended to take a two-dimensional array as an input and does not return a value.
This method’s purpose is to print the items of the two-dimensional array given to it. On the console, the command obj.show(x); shows the elements of a two-dimensional array.
With sample programs, I hope this article has addressed nearly all of the essential aspects of providing arrays to methods in Java. I hope you’ve figured out how to send one-dimensional and two-dimensional arrays to a Java function.
Thank you for taking the time to read!!!
We’re going to look at how to use “Object[]” and “Object” in Java to pass arrays and objects. These aren’t the only Java containers we can use to pass arrays and objects, but we’re going to use these because they are the most flexible.. Read more about how to pass array by reference in java and let us know what you think.
Frequently Asked Questions
How do you pass an array to a method in Java?
You can use the method call notation, as in methodName.parameterList().
How do you pass an array to a method?
You can pass an array to a method by using the [] operator.
Related Tags
- java array methods
- passing 2d array to function in java
- java return string array from method
- passing array of objects as arguments in java
- how to pass array as parameter in java