Returning Arrays in Java

To return an array in Java, you can simply specify the array type as the return type of the method. Then, you can return the array using the return statement.

Here is an example of a method that returns an array of integers:

public int[] getArray() {
    int[] array = {1, 2, 3, 4, 5};
    return array;
}

This method defines an array of integers and returns it.

You can also return an array of objects in the same way. For example:

public String[] getArray() {
    String[] array = {"apple", "banana", "cherry"};
    return array;
}

This method defines an array of strings and returns it.

To use the returned array in the calling code, you can simply assign it to a variable or pass it to another method. For example:

int[] array = getArray();

This code calls the getArray() method and assigns the returned array to the array variable.

You can also pass the returned array to another method as an argument. For example:

printArray(getArray());

This code calls the getArray() method and passes the returned array to the printArray() method as an argument.