Returning Multiple values in Java Last Updated : 13 Sep, 2018 Comments Improve Suggest changes Like Article Like Report Java doesn't support multi-value returns. We can use following solutions to return multiple values. If all returned elements are of same type We can return an array in Java. Below is a Java program to demonstrate the same. Java // A Java program to demonstrate that a method // can return multiple values of same type by // returning an array class Test { // Returns an array such that first element // of array is a+b, and second element is a-b static int[] getSumAndSub(int a, int b) { int[] ans = new int[2]; ans[0] = a + b; ans[1] = a - b; // returning array of elements return ans; } // Driver method public static void main(String[] args) { int[] ans = getSumAndSub(100, 50); System.out.println("Sum = " + ans[0]); System.out.println("Sub = " + ans[1]); } } Output: Sum = 150 Sub = 50 If returned elements are of different types Using Pair (If there are only two returned values) We can use Pair in Java to return two values. Java // Returning a pair of values from a function import javafx.util.Pair; class GfG { public static Pair<Integer, String> getTwo() { return new Pair<Integer, String>(10, "GeeksforGeeks"); } // Return multiple values from a method in Java 8 public static void main(String[] args) { Pair<Integer, String> p = getTwo(); System.out.println(p.getKey() + " " + p.getValue()); } } If there are more than two returned values We can encapsulate all returned types into a class and then return an object of that class. Let us have a look at the following code. Java // A Java program to demonstrate that we can return // multiple values of different types by making a class // and returning an object of class. // A class that is used to store and return // three members of different types class MultiDivAdd { int mul; // To store multiplication double div; // To store division int add; // To store addition MultiDivAdd(int m, double d, int a) { mul = m; div = d; add = a; } } class Test { static MultiDivAdd getMultDivAdd(int a, int b) { // Returning multiple values of different // types by returning an object return new MultiDivAdd(a * b, (double)a / b, (a + b)); } // Driver code public static void main(String[] args) { MultiDivAdd ans = getMultDivAdd(10, 20); System.out.println("Multiplication = " + ans.mul); System.out.println("Division = " + ans.div); System.out.println("Addition = " + ans.add); } } Output: Multiplication = 200 Division = 0.5 Addition = 30 Returning list of Object Class Java // Java program to demonstrate return of // multiple values from a function using // list Object class. import java.util.*; class GfG { public static List<Object> getDetails() { String name = "Geek"; int age = 35; char gender = 'M'; return Arrays.asList(name, age, gender); } // Driver code public static void main(String[] args) { List<Object> person = getDetails(); System.out.println(person); } } Output: [Geek, 35, M] This article is contributed by Twinkle Tyagi. Comment More infoAdvertise with us Next Article Returning Multiple values in Java K kartik Follow Improve Article Tags : Java Computer Science Fundamentals Practice Tags : Java Similar Reads Short shortValue() Method in Java The shortValue() is an inbuilt method of the Short class in Java and is used to return the Short value of this value. Syntax: public short shortValue() Parameters: The method does not take any parameter. Return Value: The method returns the numeric value represented by this object after conversion t 2 min read Integer shortValue() Method in Java The Integer.shortValue() is an inbuilt method of java.lang which returns the value of this Integer in the short type . Syntax: public short shortValue() Parameters: The method does not take any parameters. Return Value: The method returns the integer value represented by this object after converting 2 min read Long longValue() Method in Java The java.lang.Long.longValue() is an inbuilt method of the Long class in Java which returns the value of this Long object as a long after the conversion.Syntax: public long longValue() Parameters: This method does not take any parameters.Return Value: This method will return the numeric value repres 2 min read Java Methods Coding Practice Problems Methods in Java help in structuring code by breaking it into reusable blocks. They improve readability, maintainability, and modularity in programming. This collection of Java method coding practice problems covers functions with return values, functions with arguments, and functions without argumen 1 min read Integer reverse() Method In Java The java.lang.Integer.reverse() is an inbuilt method in Java and is used to return the reverse order of the bits in the two's complement binary representation of the specified int value. Syntax: public static int reverse(int a) Parameters: The parameter a is an integer value whose bits are to be rev 2 min read Like