How to use an output parameter in Java? [duplicate] How to use an output parameter in Java? [duplicate] java java

How to use an output parameter in Java? [duplicate]


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).

Related questions


Code sample

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!"    }}

See also


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- and o- prefix (e.g. iAddress, oPerson) is unnecessary and unidiomatic.


A more general solution

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.


Java does not support output parameters. You can use a return value, or pass in an object as a parameter and modify the object.


You can either use:

  • return X. this will return only one value.

  • return object. will return a full object. For example your object might include X, Y, and Z values.

  • pass array. arrays are passed by reference. i.e. if you pass array of integers, modified the array inside the method, then the original code will see the changes.

Example on passing Array.

void methodOne{    int [] arr = {1,2,3};    methodTwo(arr);    ...//print arr here}void methodTwo(int [] arr){    for (int i=0; i<arr.length;i++){         arr[i]+=3;    }}

This will print out: 4,5,6.