• Register
1 vote
31 views

Problem:

I am a beginner level java student. So my question might be very easy for you guys. I am just wondering if it is possible to return multiple values in a single program?

If it is possible, can you guys tell me the process, please?

-Best regards

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

Yes, in java you can return multiple values in a single program. Though it depends on how and what kind of data are you wanting to return. As per your sayings, I am showing you the way to do it for the same data-type by returning an array.

class Test { 

    static int[] getSumAndSub(int a, int b) 
    { 
        int[] ans = new int[2]; 
        ans[0] = a + b; 
        ans[1] = a - b; 
  
        return ans; 
    } 
  
    public static void main(String[] args) 
    { 
        int[] ans = getSumAndSub(20, 10); 
        System.out.println("Sum = " + ans[0]); 
        System.out.println("Sub = " + ans[1]); 
    } 
}

Output:

Sum = 30
Sub = 10

If you want to return a different type of data in a single program you can use the object class like below:

import java.util.*; 
  
class Test { 
    public static List<Object> getDetails() 
    { 
        String name = "Name"; 
        int age = 25; 
        char gender = 'M'; 
  
        return Arrays.asList(name, age, gender); 
    } 
  
    public static void main(String[] args) 
    { 
        List<Object> person = getDetails(); 
        System.out.println(person); 
    } 
}

In this case, the output will be:

[Name, 25, M]

 

13 9 6
94,240 points

Related questions

1 vote
1 answer 31 views
31 views
Problem: I am an intermediate level java programmer, and I can write codes in java bit fluently. Recently, I am learning code in python. If anybody could tell me how can I return multiple values in python it would be very helpful to me. I know the ways how to do it in java but not in python. Thanks
asked Mar 10, 2020 Gavin 15.3k points
0 votes
1 answer 24 views
24 views
I need to know how to return multiple values from a function in Java.
asked Oct 3, 2020 Daniel Anderson 4k points
0 votes
1 answer 4 views
4 views
Problem: I am a noob and my question is simple. What does return 0 do in every C++ program? Is it important to end each program with return 0 and what if we use any number other than 0 in the return statement? I am confused so looking forward to a detailed answer. Thanks!
asked 3 days ago Code Learner 6k points
0 votes
1 answer 14 views
14 views
Problem; what does return do in c++
asked Feb 6 kaash_kashi 1.2k points
0 votes
1 answer 105 views
105 views
Problem: Hi there! I am a bit curious about what happens if I skip the return type in function declaration. Does the compiler generate error or the program executes normally? If it executes normally, what return type is used as function return type by default?
asked Nov 28, 2020 Code Learner 6k points
0 votes
1 answer 18 views
18 views
Problem: "Not all code paths return a value" means that inside a function that's supposed to explicitly return something why it's not void or a constructor/IEnumerator)?how compiler found a way to hit the end of the function without a return statement telling it what it's supposed to return?
asked Dec 9, 2020 Mashhoodch 9.3k points
0 votes
1 answer 15 views
15 views
Problem: I am working with functions in C++. I am encountering an error that say &ldquo;not all code paths return a value&rdquo; while calling the following function: public static int divide(int a, int b) { int div = a / b; Console.Write(div); } I have no idea whats happening. How to resolve the error and why is occuring??
asked Oct 22, 2020 Code Learner 6k points
0 votes
1 answer 74 views
74 views
Problem : I want to create a message with a Yes or No button. After that a window will appear with a certain message which will depend on if the user clicked Yes or No. Following is my code: public class TestJoptionPane { public static void main(String[] args) ... if(true){ JOptionPane.showMessageDialog(null, "HELLO"); } else { JOptionPane.showMessageDialog(null, "GOODBYE"); } System.exit(0); } }
asked Nov 20, 2019 peterlaw 6.9k points
0 votes
1 answer 18 views
18 views
Problem: Is it possible for us to implement a HashMap with one key and two values. Just as HashMap?Please do help me, also by telling (if there is no way) any other way to implement the storage of three values with one as the key?
asked Feb 7 ahmedadel9090 2.6k points
0 votes
1 answer 17 views
17 views
Problem: I have the following function : void getdata(int arr[], int n) { for (int i = 0; i < n; i++) { int a = srand(time(NULL)); arr[i] = a; } } And I call it in main: getdata(arr, 1024); I get "void value not ignored as it ought to be" but I don't understand what is wrong.Why do I get this error?
asked Dec 21, 2020 Mashhoodch 9.3k points