Java

Does Java pass Array by Reference to a Method?

Does Java pass Array by Reference to a Method? Yes, and that is the only way to pass an array to a function in Java. In Java, a function is called a method, and it is a member of a class. More good news! A method in Java can return an array, and this is by reference. The identifier of the array is a reference.

Recall: A typical Java program file has a main class with the main() method. The name of the main method is main. However, the name of the main class is not the main. It is the name chosen by the programmer. It is also the name of the file. This main class can have other members (properties and functions). Other classes outside the main class can have their properties and methods. In this article, the main method is not concerned with passing and returning the array. In Java, a property is called a field.

This article illustrates how to pass an array to a method and return an array from a method by reference. The array class does not need to be imported for this program.

Method in a Separate Class

Passing by Reference

In this section of the article, the method to receive the array is in a separate class, not the main class. An example of such a class is:

    class AClass {
        char func(char[] chs) {
            return chs[2];
        }
    }

The name of this extra class is Aclass. It has only one member, which is a method. The name of the method is func. It has only one parameter, which is an array declaration. It returns a char. The character returned is at the third position of the array received (index 2).

In the main() method, the following code segment can be employed:

char[] arr = {'A', 'B', 'C', 'D'};
AClass obj = new AClass();
            char ret = obj.func(arr);
System.out.println(ret);

The first statement in this code segment defines the array. The second statement declares the object, obj from the class, AClass. The next statement calls the method of the object, with the array name (reference) as argument. The last statement prints out the third character (index 2) of the array received by the method called. The output would be C.

These two code segments show how an array is passed to a method by reference.

Returning by Reference

The above class is modified for the method to return an array by reference, as follows:

    class AClass {
char[] func(char[] chs) {
            return chs;
        }
    }

The return expression in the method signature is char[] and no longer, just, char. The return expression of the return statement is now the name of the array in the parameter and is no longer an array element.

The corresponding code segment in the main() method is modified to be:

char[] arr = {'A', 'B', 'C', 'D'};

AClass obj = new AClass();

char[] ret = obj.func(arr);

System.out.println(ret[2]);

The return type of the method call is an array and not just char. The array is still passed as an argument in the same way as before. The last statement here prints an element value of the return array.

The above two programs show how an array can be passed by reference and how an array can be returned by reference.

Method in Main Class

Passing by Reference

In this section of the article, the method of receiving the array is in the main class, not in a separate class. An example of such a method is:

static char func(char[] chs) {

  return chs[2];

}

Notice that the method of interest now is static because it will be called from the main method that is also static and is also in the main class. The name of the method is func. It has only one parameter, which is an array declaration. It returns a char. The character returned is at the third position of the array received (index 2).

In the main() method, which is static, the following code segment can be employed:

char[] arr = {'A', 'B', 'C', 'D'};

char ret = func(arr);

System.out.println(ret);

The output would be C. The first statement in this code segment defines the array. No object is declared here, as there is no separate class. The next statement calls the same class method, the main class, with the array name (reference) as an argument. The last statement prints out the third character (index 2) of the array received by the method called.

These two code segments show how an array is passed to a method by reference.

Returning by Reference

The above method is modified, to return an array by reference, as follows:

static char[] func(char[] chs) {

return chs;

}

The return expression in the method signature is “static char[]” and no longer “static char”. The return expression of the return statement is now the name of the array in the parameter and is no longer an array element.

The corresponding code segment in the main() method is modified to be:

char[] arr = {'A', 'B', 'C', 'D'};

char[] ret = func(arr);

System.out.println(ret[2]);

The first statement here is the declaration of the array. The return type of the method call is an array and not just char. The array is still passed as an argument in the same way as before. The last statement prints an element value of the return array.

These two programs show how an array can be passed by reference and how an array can be returned by reference.

Conclusion

Yes, and that is the only way to pass an array to a method in Java: declare the parameter as an array, and use the array name in the method call. In Java, a function is called a method, and it is a member of a class. More good news! A method (in Java) can return an array, which is also by reference. The identifier (name) of the array is a reference. Let the method definition (implementation) have the array type, as a parameter, in the method signature to achieve these. The expression for the return statement in the method definition is just the array name. The return expression for the method signature is the array type. The receiving expression should be of the array type in the method call statement.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.