Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

return two values in 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]);
    }
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #return #values #java
ADD COMMENT
Topic
Name
4+2 =