How to Return an Array in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

An array is a collection of homogeneous elements that are accessed with the help of the index position. We can also pass an array as a parameter as well as return the array from the particular user-defined or predefined function. To return an array in Java, you can simply include the array as the return type of a method.

How to Return an Array in Java?

Methods or functions have the property to divide the program into smaller modules. Methods can also take arguments according to the required operation or return a particular value or set of values after the operations.

The method can also take an array as an argument and returns the reference of the array. The reference is a base address of the array that specifies the location of an array in the memory.

There are two ways to pass arguments in the method body, pass by value, and pass by reference. The array follows the rule of pass-by-reference when they pass as an argument in the particular method.

The return statement is used in the user-defined or predefined method to return an array from the method.

Let's understand how to return an array in java using an example.

Program to Return an Array in Java

In the below program, we are returning an array of the String from the user-defined method using the return statement.

The return statement returns the reference of the String array to the caller statement.

Output:

The [Ljava.lang.String;@2c7b84de is the reference of the returned array from the ArrayReturn() method. The reference depicts the memory location of the array.

Example: Passing Array as an Argument and Returning an Array

Let's understand how to pass an array as an argument and return from the method using an example.

Note: Since passing an array as an argument in a function is done using pass-by-reference, hence any kind of changes on the array within the method will be reflected in the original array.

In the below example, we are passing an array of int type as a parameter to the method named as an update, increasing all the values of an array by 1, and after this returning the array using the return statement.

Output:

Example: Returning an Array of Objects from a Function

In the below example, we are returning an array of objects that belong to the Scaler class from the user-defined method i.e Scaler.

The return type of this method is Scaler[] which means it is returning an array of Objects belonging to the Scaler class.

Let's understand this using an example, the Scaler class contains two instance values i.e first and second of int type.

Output:

Conclusion

  • The return statement is used to return an array in java.
  • The return statement returns the reference of an array to the caller statement.