• Register
1 vote
45 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,260 points

Related questions

1 vote
1 answer 38 views
38 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 8 views
8 views
Problem: I need someone to understand this problem and find out the solution... can a function return multiple values python
asked Mar 26 Ifra 36.4k points
0 votes
1 answer 8 views
8 views
Problem: I need assistance in resolving this problem: what types of joins will return the unmatched values from both tables in the join?
asked Apr 1 rakib1 51.5k points
0 votes
1 answer 9 views
9 views
Problem: how to I Solve this: error: what does the return function do in python HELP! I need help understanding this: what does the return function do in python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 11 Mashhoodch 13k points
0 votes
1 answer 16 views
16 views
Problem: My question is simple. Why do we use return 0 in a C/C++ program if it does not do anything? For example, if I have a program whose output is 100 somehow, then I use return 0 after the print command, the output will still be 100. Then, what is the use of return 0?
asked Mar 2 Code Learner 9.8k points
0 votes
1 answer 27 views
27 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 Feb 24 Code Learner 9.8k points
0 votes
1 answer 26 views
26 views
Problem; what does return do in c++
asked Feb 6 kaash_kashi 1.2k points
0 votes
1 answer 538 views
538 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 9.8k points
0 votes
1 answer 5 views
5 views
Problem: What if I write return statement in constructor? Is it standard conformant? struct A { A() { return; } }; The above code compiles fine, without any error at ideone. But the following code doesn't: struct A { A() { return 100; } }; It gives ... forbids the second one? Is there any explicit statement? Is the return type in the first example void? Is there any implicit return type at all?
asked Apr 24 Humaira ahmed 50.7k points
1 vote
1 answer 6 views
6 views
Problem: I have a list : apple 5 sausage 2 banana 2 cheese 1How can I return it in descemding order . Like &ndash; apple , sausage , banana, cheese
asked Apr 21 Raj Tyagi 14.9k points