Reputation: 18171
I would like to make function that could return several primitive data types to caller.
I know I can return function result for first primitive, but how to return primitive in function param?
public boolean myFunc( boolean outAnotherPrimitive)
{
outAnotherPrimitive = true; //need to return value to caller
return true;
}
Is it only way to return primitive to wrap it into Object like Integer or Boolean?
Upvotes: 0
Views: 118
Reputation: 77904
Is it only way to return primitive to wrap it into Object like Integer or Boolean?
Not at all,
I think its not good practice to convert your variables to Object and after fetch them back with cast or instanceof
.
example:
OtherClass
public class OtherClass{
....
public void myFunc( boolean anotherPrimitive, MyinterfaceItf myItf)
{
boolean bool = false;
int a = 1;
myItf.onFinish(bool, a)
}
....
}
MyClass:
public class MyClass implements MyinterfaceItf {
....
private void foo()
{
MyinterfaceItf itf = this;
myFunc(true, itf );
}
@override
public void onFinish(bool, a){
// here you can get your primitive data
}
}
interface
public interface MyinterfaceItf{
public void onFinish(bool, a);
}
example:
private boolean bool = false;
private int num = 0;
public boolean myFunc( boolean anotherPrimitive)
{
bool = anotherPrimitive;
num = 10;
//....
}
example:
public class NewClass{
private boolean bool = false;
private int num = 0;
//...
public void setBool(boolean flag){
this.bool = flag;
}
}
public boolean myFunc( boolean anotherPrimitive, NewClass newClass)
{
return newClass.setBool(true);
}
(I wrote it in local editor, sorry for syntax)
Upvotes: 1