How to return multiple values from a method in Java

3 Answers

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

 



answered Aug 8, 2019 by avibootz
0 votes
final class Test
{
    public String s;
    public int i;
    public char ch;
 
    public Test(String s, int i, char ch) {
        this.s = s;
        this.i = i;
        this.ch = ch;
    }
}
 
public class MyClass
{
    public static Test getDetails() {
        String s = "Java";
        int i = 84672;
        char ch = 'z';
 
        return new Test(s, i, ch);
    }
 
    public static void main(String[] args)
    {
        Test test = getDetails();
 
        System.out.println(test.s);
        System.out.println(test.i);
        System.out.println(test.ch);
    }
}




/*
run:

Java
84672
z

*/

 



answered Jun 11, 2021 by avibootz
0 votes
import java.util.List;
import java.util.Arrays;

public class MyClass {
    public static List<Object> m() {
        String s = "Java";
        int n = 23876;
        char ch = 'z';
  
        return Arrays.asList(s, n, ch);
    }
    public static void main(String args[]) {
        List<Object> list = m();
        
        System.out.println(list);
    }
}
  
  
  
/*
run:
  
[Java, 23876, z]
  
*/

 



answered Jan 3, 2022 by avibootz

Related questions

3 answers 310 views
3 answers 347 views
3 answers 146 views
1 answer 115 views
1 answer 103 views
1 answer 141 views
141 views asked Jul 25, 2019 by avibootz
...