Returning an array in Java

Java 8Object Oriented ProgrammingProgramming

Yes, an array can be returned from a java function. See the example below −

Example

public class Tester {
   public static void main(String[] args) {
      int[] array = getData();
      
      for(int i: array) {
         System.out.println(i);
      }
   }
   public static int[] getData() {
      int[] dataArray = {1, 2, 3, 4};
      return dataArray;
   }
}

Output

1
2
3
4
raja
Published on 06-Feb-2018 10:06:25
Advertisements