How to return two values from a method in Java

3 Answers

0 votes
public class MyClass {
    public static int[] getValues() { 
        int[] arr = new int[2]; 
          
        arr[0] = 23; 
        arr[1] = 9; 
  
        return arr; 
    } 
    public static void main(String args[]) {
        int[] result = getValues(); 
            
        System.out.println(result[0]);
        System.out.println(result[1]);
    }
}
   
   
   
   
/*
run:
   
23
9
 
*/

 



answered Aug 8, 2019 by avibootz
edited Nov 4, 2023 by avibootz
0 votes
public class MyClass {
    public static int[] getValues() { 
        int n1 = 87;
        int n2 = 91;
         
        return new int[] {n1, n2};
    } 
    public static void main(String args[]) {
        int[] result =  getValues(); 
             
        System.out.println(result[0]);
        System.out.println(result[1]);
    }
}
    
    
    
    
/*
run:
    
87
91
  
*/

 



answered Aug 8, 2019 by avibootz
edited Nov 4, 2023 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    public static int[] getValues() {
        return new int[] {34, 908};
    }
    public static void main(String args[]) {
        int[] array = getValues();

        System.out.println(Arrays.toString(array)); 
    }
}
    
   
   
    
/*
run:
   
[34, 908]
 
*/

 



answered Nov 4, 2023 by avibootz

Related questions

3 answers 236 views
1 answer 135 views
2 answers 212 views
1 answer 114 views
1 answer 102 views
...