How to Return an Array in Java

In this tutorial, we will learn how to return an array in java.

Returning an array in Java

Step 1: You have to declare the return type of the method as an array of the data type you want to return.

return-type method-name(arguments) {...}

Step 2: Write a return statement with the array you want to return.

return arrRefVar;

Note: the method will return a reference to an array.


public class Demo {
  //getArr() method has return type of int array 
  static int[] getArr() {
    //Declaring, instantiating and initializing the array
    int arr[] = {1,2,3,4,5};
    //returning the array
    return arr;
  public static void main(String args[]) {
    int a[];
    //calling the method to get the array
    a = getArr();
    //Printing the array which was returned from getArr() method
    System.out.println("The array returned is: ");
    for(int i=0; i<a.length; i++) {
      System.out.println("a["+i+"] = "+a[i]);


The array returned is: 
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5

Here is a tutorial you might like: How to copy elements from one array to another in Java.
Implementation of Associative Array in Java

Leave a Reply

Your email address will not be published. Required fields are marked *