0

Say I have a java function as follows,

public static int my(int a, int b)
{

    int c = a + b;
    return c;

    String d = "Some Data";
    return d;

    float f = a/b;
    return f
}

So, how do I get the 3 return values separately?

all the values are of different data types.

I've seen this question and this question but couldn't understand properly.

6
  • 5
    create a class which can hold 4 values a,b,c,d. in the function my create an instance of the class and set the values of a,b,c,d and return the object Commented Apr 21, 2014 at 9:05
  • What part of the answers to those linked questions was unclear? Commented Apr 21, 2014 at 9:05
  • You can only return once, and you can't return multiple values. The answers you linked provide solutions. Returning an int[] array or some collection also works. Commented Apr 21, 2014 at 9:06
  • Doing things using an object is more reliable and better anyway. Commented Apr 21, 2014 at 9:07
  • you can also create list<Integer> or map<operation,Integer> and return value. Commented Apr 21, 2014 at 9:08

6 Answers 6

4

any function can only return one value. What you can do is to create an objet containing all your answers and return this object.

class ResultObject
{
   public int c;
   public int d;
   public int e;
   public int f;
}

in your function white

public static ResultObject my(int a, int b)
{
ResultObject resObject = new ResultObject();
resObject.c = a + b;

resObject.d = a*b;

resObject.e = a-b;

resObject.f = a/b;
return resObject;
}

You can return only one value. You have to make that value to "contain" other values.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes this is the right way. And use of getter methods will be more appreciated in this regard
say I want to access only f..how to do that in my action class?
ResultObject r = my(5, 10); System.out.println(r.f);
1

There are two ways.

  1. If you returning uniform values, e.g. hundred values for temperature over a period of time - use arrays.
  2. If values are non-uniform, e.g. first name, last name and age - introduce a new class.

Reason for this is that Java is a strongly-typed programming language. Wanna describe a new data structure - write a new class.

Comments

1

return array of int.. e.g. int[]...

public static int[] my(int a, int b) {
    int res[] = new int[4];

    int c = a + b;
    res[0] = c;

    int d = a * b;
    res[1] = d;

    int e = a - b;
    res[2] = e;

    int f = a / b;
    res[3] = f;

    return res;
}

2 Comments

new int[4] for four values.
you will get indexOutOfBound exception. new int[3]. updated your answer.
0

You can try something like this

  public static int[] my(int a, int b) { // use an array to store values
    int[] arr = new int[4];
    int c = a + b;
    arr[0] = c;

    int d = a * b;
    arr[1] = d;

    int e = a - b;
    arr[2] = e;

    int f = a / b;
    arr[3] = f;

    return arr; // return values 
}

1 Comment

Please check my edit, what to do if the return types are of different data types?
0

You can return only one element but the element may be array or list.you may return list of values.(some exercise). I hope this may bring some solution.

Comments

0
public class DataStorage{
         private int a;
         private String data;
         private float f;

         public DataStorage(int a, String data, float f){
             this.a = a;
             this.data = data;
             this.f = f;

         }

         /* standard get/set method. */
     }

    public static DataStorage my(int a, int b)
    {

       int c = a + b;


       String d = "Some Data";


       float f = a/b;


        DataStorage dataStorage = new DataStorage(c,d,f);
        return dataStorage;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.