Can a Java method return multiple types?
Java does not support multiple return values. However, sometimes it is required to return multiple values. For that, we can use the following solutions. Show
Case 1: If all of the returned values are the sameIf all the values that have to be returned are the same, then we can use an array. For example, two numbers are given, and it is required to perform the addition, subtraction, multiplication and division on these numbers. In such a scenario, we can use an array. Observe the following program. FileName: ReturnMultipleValues.java Output: The sum of numbers 6 and 3 is: 9 The difference of numbers 6 and 3 is: 3 The multiplication of numbers 6 and 3 is: 18 The division of numbers 6 and 3 is: 2 Complexity Analysis: Since the program is using a for-loop for computing the sum of elements, the time complexity of the program is O(n), where n is the total number of elements present in the array. The space complexity of the program is constant, i.e., O(1). Case 2: If we have to return two values of different typesIn case we have the two values of the different types, we can use Pair. FileName: ReturnMultipleValues1.java Output: Case 3: If we have to return more values of different typesIn that case, we can use a class. Observe the following program. FileName: ArithmeticOperation.java Output: Statement: Performing Arithmetic Operation The Multiplication of the numbers 29 and 20 is: 580 The Division of the numbers 29 and 20 is: 1.45 The Addition of the numbers 29 and 20 is: 49 You can return only one value in Java. If needed you can return multiple values using array or an object. ExampleIn the example given below the calculate() method accepts two integer variables performs the addition subtraction, multiplication and, division operations on them stores the results in an array and returns the array. public class ReturningMultipleValues { static int[] calculate(int a, int b){ int[] result = new int[4]; result[0] = a + b; result[1] = a - b; result[2] = a * b; result[3] = a / b; return result; } public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter the value of a: "); int a = sc.nextInt(); System.out.println("Enter the value of b: "); int b = sc.nextInt(); int[] result = calculate(a, b); System.out.println("Result of addition: "+result[0]); System.out.println("Result of subtraction: "+result[1]); System.out.println("Result of multiplication: "+result[2]); System.out.println("Result of division: "+result[3]); } } OutputEnter the value of a: 20 Enter the value of b: 10 Result of addition: 30 Result of subtraction: 10 Result of multiplication: 200 Result of division: 2
Updated on 30-Jul-2019 22:30:20
In Java, the methods can return only one value at a time. In other words, Java doesn't support the return of multiple values from a method directly. However, in some applications it is required to return multiple value from a function. In order to achieve this goal, we can use some other features of Java like an Array, a Pair, a List, etc. Depending on the type and nature of data, we need to choose an appropriate approach to accomplish this task. We can use any of the five shown approaches as per the program requirement to return multiple values in Java.
Return an Array of specific type or objectIn order to return multiple values in Java, we can use an array to stores multiple values of the same type. However, we can use this approach only if all the values to be returned are of the same type. This is the simplest approach to return both primitive type data
as well as reference data types.
Output
Return using the Pair class of util PackageFrom Java 8 onwards, We can use this approach to return multiple values in Java. Here, we will represent the name-value pairs using the
Pair class available in the Example :
Output
Return a String object with a delimiterIn this approach, we will return multiple values by constructing the result as a single string such that a delimiter separates the different values. On the other hand, after receiving the string we have to split it using the same delimiter. However, this approach is not so efficient as we may need to convert the string to a numeric value for certain data value. Example :
Output
Return a Custom class objectThis is the commonly used approach if we want to return multiple values of different types in Java. We simply encapsulate all returned types into a class and then return an object of that class. Such a class will be a simple java class with a public member variables and a public constructor if needed. Example : In this example, we will define a Student class consisting of class variables. Thereafter, we will define a method
Output
Return using a Collection object ListThis approach to return multiple values in Java is an alternative to returning an array. Here, we will return a List Interface of a Collection object. The collections framework consists of a wide variety of a classes and an interfaces.
Output
SummaryThe knowledge of returning multiple values in Java is very useful while working on real time applications. In this tutorial, we covered five different approaches to return multiple values in Java. As per the requirement of an application, we can choose an appropriate approach to return the values. We learned in detail about this approaches with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on returning multiple values from a function in Java. ReferencesPair class of util Package Didn't find what you were looking for? Perform a quick search across GoLinuxCloud Can a method have multiple return types in Java?You can return only one value in Java. If needed you can return multiple values using array or an object.
How can I return multiple data types in Java?If all the values that have to be returned are the same, then we can use an array. For example, two numbers are given, and it is required to perform the addition, subtraction, multiplication and division on these numbers. In such a scenario, we can use an array. Observe the following program.
Can a Java method return more than one object?Yes, we can return multiple objects from a method in Java as the method always encapsulates the objects and then returns.
Can a function return multiple data types?If we want the function to return multiple values of same data types, we could return the pointer to array of that data types. We can also make the function return multiple values by using the arguments of the function.
|