What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Return an Array in Java? (from a Method)

  • Feb 17, 2021
  • 4 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Arkadyuti Bandyopadhyay
How to Return an Array in Java? (from a Method)

Often, we find the need to work with multiple instances of the same data type. Declaring several instances of the data type is not only inefficient but also very difficult to work with. That’s exactly where the array data structure comes in handy. The array data structures houses multiple instances of the same data type under one named storage location (or variable). Arrays are some of the most versatile data structures in Java. They allow instant access to every element in the array through the use of indexing. This is only possible because arrays are allocated contiguous spaces in the memory for the storage of data. But we should also learn in Java, Return Array.

The basics come down to the input and output of an array as well as passing it to other functions for manipulations (and returning it from these functions). Our article on how to return an array in Java would help you get accustomed to the use of the array data structure in your Java programs. This is a complete guide on how do you pass and return an array in Java?

Can an Array be returned in Java?

Java allows arrays to be passed as parameters for other functions. Arrays are also a valid way of returning data especially when you’re dealing with a multitude of data.

There’s only one small thing to keep in mind. Java has two ways of passing parameters to functions: call by value and call by reference. Call by value sends only the scalar value of the variables to the parameters of the function, while call by reference passes a reference to the original variables to the parameters of the function.

What’s the difference? Well, if any modification is made to the parameter in the function, changes don’t reflect the value of the original variable being passed in case of call by value. In case of call by reference, this is not the case; any changes done to the variable will be reflected in the original variable being passed. Java always passes arrays and vectors by reference, so any modification to the array elements in another function would mean that the original array would also be modified similarly.

How to Pass Array to a method in Java?

Parameter list is structured accordingly. It is always wise to send the length of the array to the function because the calculation of length within the function would be pretty tedious.

class Array
{
    public void returnArr(int arr[],int len)
    {
        for (int i = 0; i < len ; i++)
        {
            System.out.print(arr[i]+" ");
        }
    }

    public static void main(String args[])
    {
        Array obj = new Array();
        int arr[] = { 1, 2, 3, 4, 5 };
        obj.returnArr(arr,arr.length);
    }
}

How to Return Array from a method in Java?

Returning arrays from a function are pretty simple. A method that returns an array needs to have the return type of the function set to the appropriate data type for the array. Here is code for how to return an array in java:

class Array
{
    public int[] returnArr()
    {
        int arr[] = { 1, 2, 3, 4, 5 };
        return arr;
    }

    public static void main(String args[])
    {
        Array obj = new Array();
        int a[] = obj.returnArr();
        for (int i = 0; i < a.length ; i++)
        {
            System.out.print(a[i]+" ");
        }
    }
}

Output:

output

Conclusion

Arrays are very easy to understand and work with. One should just keep a few pointers in mind while working with arrays in Java. Now you know how to return an array in Java should prove pretty useful.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Arkadyuti Bandyopadhyay
Hi, I am Arkadyuti, a developer and open source enthusiast with quite some experience in content writing. I wish to make the switch to a full-stack developer one day.