+ 12
How to return multiple value from a method() when we have different data type in java? 😭😭😭
Here's my method static int myMethod(int a, int b){ int sum = a+b; int sub = a-b; double multi = a*b; return😭😭😭 } I want to return, sum, sub, multi. How to do it give me an example based on my code.
22 Answers
+ 7
Object[] operations(int a, int b, String c){
return new Object[]{a + b, a - b, c);
}
Since this returns a new Object array I could do:
Object[] math = operations(3, 2, "Example");
math[0]; // this is 3+2
math[1]; // this is 3-2
math[2]; // this is Example
I would probably use seperate methods like:
int sum(int a, int b){}
int subtract(int a, int b){}
etc.. Instead.
Or I'd probably make a class instead of returning an Object array.
But Object array should be fine for this nonetheless if you need it in one method, as long as you know what each index is.
+ 8
@Daniel Thank you 😄 But what if there different data type?
static int myMethod (int a, int b){
int sum = a+b;
int sub = a-b;
double mul = a*b;
return😭😭😭
}
// i have edited my question.
+ 8
@Restoring you mentioned about using generic for this kind of purpose can you drop some link, I wanna learn that way too.
+ 7
@Nitin method overloading need two or more method of same name,
But different in argument either in number, or in type.
And i have just one method. So it's not method overloading at all.
+ 7
@Restoring I totally agree, But i had limitations to do it by just using 1 single method.
+ 6
I assume you want to do this in a single method.
You can only return the type specified by the return value of the method.
int myMethod(){}
This MUST return an int.
You could use an Object array and return that.
Object[] myMethod(int a, int b, double c){
return new Object[] {a + b, a - b, a * c};
}
Or
You could create your own object that has those instance variable, and use either a getter or public variable to access them. This would however use more than one method.
Or
Perhaps you could make the return type a generic and return an Object array.
+ 6
@Nitin you don't understand I'm not trying to overload any method here all i want is that i want to return multiple value of different datatype like say string, int, double from one single method.
Thanks you very much for the answer @Restoring.
I will try by Object[] array way.
+ 5
@Nitin
You can solve this by overloading you are right, but I think she just wanted to do it in one method.
IE: returning multiple different typed variables.
If not, then @Fuyuka I suggest using multiple methods 😜.
+ 5
The first way that i have told earlier needs to have same method and i accept it. But what about the second way of method overloading.
Okay @rrestoring.
+ 5
@Nitin How many time i have to told you I'm not doing method overloading here can't you see i have just one method.
So it's not method overloading at all.
Method overloading need two or more method of same name.
But different in argument either in number, or in type.
I know method Overloading and Overriding already.
Yes i can make the same program with method overloading you are right here but, The task was to do it using just one method only.
If you do not understand me still then leave it, I knew my English is so poor maybe you can't get what I'm saying.
Arigato(I'm sorry for this)
@Restoring you were quite late but thank you for the favour😄
+ 4
I have a doubt @Fuyuka i have read method overloading somedays ago.
and it can be done by two ways:
1. By changing the argument.(i.e if two or three arguments)
2. By changing the datatypes.(i.e. int, double etc.)
so what you are doing is second type.
Isn't it method overloading.
Tell me why i am wrong.
Just asking for curosity...
+ 4
okay we should end it here.
+ 4
@Fukuka, i got what i am misunderstanding.
Sorry for the inconvinence.
What i am trying to say is wrong. and i have deleted it.
I understand it now you can see it if want:
If there are no matching type arguments in the method, and each method promotes similar number of arguments, there will be ambiguity.
class Overloading{
void sum(int a,long b){System.out.println("a method invoked");}
void sum(long a,int b){System.out.println("b method invoked");}
public static void main(String args[]){
Overloading obj=new Overloading();
obj.sum(20,20);//now ambiguity
}
}
Sorry agin.😔
+ 4
@Fuyuka
Late response, my bad.
https://www.tutorialspoint.com/java/java_generics.htm
+ 3
static int[] myMethod (int a, int b){
int sum = a+b;
int sub = a-b;
int mul = a*b;
return new int []{sum,sub,mul};
}
+ 3
@Rrestoring faith i didn't get it, can you eleborate please...
+ 3
@Nitin
Basically the idea is that each element in the Object array stores some variable, then the method returns the whole array so you get all of them.
So array[0] could be a String.
array[1] could be a + b.
etc..
If that didn't help, can you explain which part was confusing?
If she wants to do the same operations with different types then generics is certainlly the way to go, but she just wants to return a bunch of variables with one method.
+ 3
okay i get it. (a little) Thanks..
I will research and understand it a little more.
:)
+ 2
@It Do
Passing by reference with primitive data-types is not possible in Java.
+ 2
You could always add your values to a Collection and return that, e.g. an ArrayList