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 house 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 to 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 the 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?
The 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 an 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:
Returning an array of objects in java
It is possible to return an array of objects from a method in Java. When you return an array of objects, you can encapsulate and retrieve several items as a single entity. In Java, you can return an array of objects like the following:
To return an array of items, perform the following steps:
- Declare a method with the object array you want to return as its return type. For example: Public MyClass[] getObjectArray()
- Using the new keyword, create an array of objects of the required type within the method. For example: MyClass[] myArray = new MyClass[size];
- Populate the array with desired objects.
- Using the return keyword, return the populated array.
Code to return an array of objects in JAVA:
public class ObjectArrayExample { public static void main(String[] args) { ObjectArrayExample example = new ObjectArrayExample(); MyClass[] objectArray = example.getObjectArray(); for (MyClass obj : objectArray) { // Perform operations on each object in the array obj.doSomething(); } } public MyClass[] getObjectArray() { MyClass[] myArray = new MyClass[3]; myArray[0] = new MyClass(); myArray[1] = new MyClass(); myArray[2] = new MyClass(); return myArray; } } class MyClass { // Class implementation public void doSomething() { // Method implementation } }
Does Java Pass Arrays by Reference?
Arrays are passed by value in Java, not by reference. This can occasionally cause misunderstanding about whether changes made to an array within a method affect the initial array supplied as a parameter. When an array is supplied to a method in Java, a copy of the array's reference is created and passed to the method. This signifies that the original and copied references both point to the same array object in memory.
The reference, on the other hand, is passed by value, which means that changes made to the reference within the method do not affect the original reference in the calling code.
Even though the references are duplicates, they both lead to the same underlying array object. This means that any changes made to the array items within the method will be mirrored in the original array. Because the copy of the reference and the original reference both refer to the same array object, changes to the array's contents are visible.
Why are Arrays not passed by value?
Arrays are not supplied by value in Java because they are a reference type rather than a primitive one. Primitive types such as int, char, and boolean are passed by value in Java. When a primitive value is supplied to a method, a copy is created, and any changes made to the copy do not affect the original value.
Arrays, on the other hand, are objects in Java, and objects are always provided by value. When an array is supplied to a method, a copy of the array object's reference is produced. This means that the method receives a copy of the memory address where the array is stored rather than a copy of the array members themselves.
Because the reference is supplied by value, any changes made to it within the method, such as reassigning it to a different array, will not affect the original reference in the calling code. However, the original and copied references both point to the same array object in memory. Because the copied reference and the original reference both refer to the same array object, changes to the array items within the method can be noticed outside the method.
In summary, arrays are not passed by value in Java since they are objects, and all objects, including arrays, are passed by value, which means a copy of the reference is passed. The behaviour of arrays provided by value allows changes to the array's elements to be visible outside the method while keeping the immutability of the original reference.
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.