• Register
1 vote
18 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

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 24 views
24 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 Gavin 15.3k points
0 votes
1 answer 15 views
15 views
I need to know how to return multiple values from a function in Java.
asked Oct 3 Daniel Anderson 4k points
0 votes
1 answer 54 views
54 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 5 views
5 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 Sheeza 3.5k points
3 votes
1 answer 20 views
20 views
Problem: Hello good souls, I am a new student of Java. I just started learning and trying to solve java problems. Right now, I am working with the arrays. I can do some basic things with an array. I am finding something more advance problems of an array. Thus I thought to find the ... in java? Let&rsquo;s say I have an array Integer[] num = { 2, 4, 6, 5, 8 }; Thanks in advance for your effort.
asked Mar 23 Gavin 15.3k points