Could someone please give me some sample code that uses an output parameter in function? I've tried to Google it but just found it just in functions. I'd like to use this output value in another function.
The code I am developing intended to be run in Android.
Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that's why it's called pass by value.
The output argument of a function indicates the type of object the function produces and the number of objects a function can produce. An output argument is specified as one of the following object types or values: A single data object of a type. A series of data objects of the same type.
Java passes by value; there's no out
parameter like in C#.
You can either use return
, or mutate an object passed as a reference (by value).
public class FunctionSample { static String fReturn() { return "Hello!"; } static void fArgNoWorkie(String s) { s = "What am I doing???"; // Doesn't "work"! Java passes by value! } static void fMutate(StringBuilder sb) { sb.append("Here you go!"); } public static void main(String[] args) { String s = null; s = fReturn(); System.out.println(s); // prints "Hello!" fArgNoWorkie(s); System.out.println(s); // prints "Hello!" StringBuilder sb = new StringBuilder(); fMutate(sb); s = sb.toString(); System.out.println(s); // prints "Here you go!" } }
As for the code that OP needs help with, here's a typical solution of using a special value (usually null
for reference types) to indicate success/failure:
Instead of:
String oPerson= null; if (CheckAddress("5556", oPerson)) { print(oPerson); // DOESN'T "WORK"! Java passes by value; String is immutable! } private boolean CheckAddress(String iAddress, String oPerson) { // on search succeeded: oPerson = something; // DOESN'T "WORK"! return true; : // on search failed: return false; }
Use a String
return type instead, with null
to indicate failure.
String person = checkAddress("5556"); if (person != null) { print(person); } private String checkAddress(String address) { // on search succeeded: return something; : // on search failed: return null; }
This is how java.io.BufferedReader.readLine()
works, for example: it returns instanceof String
(perhaps an empty string!), until it returns null
to indicate end of "search".
This is not limited to a reference type return value, of course. The key is that there has to be some special value(s) that is never a valid value, and you use that value for special purposes.
Another classic example is String.indexOf
: it returns -1
to indicate search failure.
Note: because Java doesn't have a concept of "input" and "output" parameters, using the
i-
ando-
prefix (e.g.iAddress
,oPerson
) is unnecessary and unidiomatic.
If you need to return several values, usually they're related in some way (e.g. x
and y
coordinates of a single Point
). The best solution would be to encapsulate these values together. People have used an Object[]
or a List<Object>
, or a generic Pair<T1,T2>
, but really, your own type would be best.
For this problem, I recommend an immutable SearchResult
type like this to encapsulate the boolean
and String
search results:
public class SearchResult { public final String name; public final boolean isFound; public SearchResult(String name, boolean isFound) { this.name = name; this.isFound = isFound; } }
Then in your search function, you do the following:
private SearchResult checkAddress(String address) { // on address search succeed return new SearchResult(foundName, true); : // on address search failed return new SearchResult(null, false); }
And then you use it like this:
SearchResult sr = checkAddress("5556"); if (sr.isFound) { String name = sr.name; //... }
If you want, you can (and probably should) make the final
immutable fields non-public
, and use public
getters instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With