Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Java passes by value; there's no <code>out</code> parameter like in C#.</p> <p>You can either use <code>return</code>, or mutate an object passed <strong>as</strong> a reference (<strong>by</strong> value).</p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2806545/does-java-have-something-like-cs-ref-and-out-keywords">Does Java have something like C#'s ref and out keywords? ?</a> (NO!)</li> <li><a href="https://stackoverflow.com/questions/40480/is-java-pass-by-reference">Is Java pass by reference?</a> (NO!) </li> </ul> <hr> <h3>Code sample</h3> <pre><code>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!" } } </code></pre> <h3>See also</h3> <ul> <li><a href="https://stackoverflow.com/questions/279507/what-is-meant-by-immutable">What is meant by immutable?</a></li> <li><a href="https://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer-in-java">StringBuilder and StringBuffer in Java</a></li> </ul> <hr> <p>As for the <a href="https://stackoverflow.com/questions/2824910/output-parameter-in-java/2825054#2825054">code</a> that OP needs help with, here's a typical solution of using a special value (usually <code>null</code> for reference types) to indicate success/failure:</p> <p>Instead of:</p> <pre><code>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; } </code></pre> <p>Use a <code>String</code> return type instead, with <code>null</code> to indicate failure.</p> <pre><code>String person = checkAddress("5556"); if (person != null) { print(person); } private String checkAddress(String address) { // on search succeeded: return something; : // on search failed: return null; } </code></pre> <p>This is how <a href="http://java.sun.com/javase/6/docs/api/java/io/BufferedReader.html#readLine%28%29" rel="noreferrer"><code>java.io.BufferedReader.readLine()</code></a> works, for example: it returns <code>instanceof String</code> (perhaps an empty string!), until it returns <code>null</code> to indicate end of "search".</p> <p>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.</p> <p>Another classic example is <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#indexOf%28int%29" rel="noreferrer"><code>String.indexOf</code></a>: it returns <code>-1</code> to indicate search failure.</p> <blockquote> <p><em>Note</em>: because Java doesn't have a concept of "input" and "output" parameters, using the <code>i-</code> and <code>o-</code> prefix (e.g. <code>iAddress</code>, <code>oPerson</code>) is unnecessary and unidiomatic.</p> </blockquote> <hr> <h3>A more general solution</h3> <p>If you need to return several values, usually they're related in some way (e.g. <code>x</code> and <code>y</code> coordinates of a single <code>Point</code>). The best solution would be to encapsulate these values together. People have used an <code>Object[]</code> or a <code>List&lt;Object&gt;</code>, or a generic <code>Pair&lt;T1,T2&gt;</code>, but really, your own type would be best.</p> <p>For this problem, I recommend an immutable <code>SearchResult</code> type like this to encapsulate the <code>boolean</code> and <code>String</code> search results:</p> <pre><code>public class SearchResult { public final String name; public final boolean isFound; public SearchResult(String name, boolean isFound) { this.name = name; this.isFound = isFound; } } </code></pre> <p>Then in your search function, you do the following:</p> <pre><code>private SearchResult checkAddress(String address) { // on address search succeed return new SearchResult(foundName, true); : // on address search failed return new SearchResult(null, false); } </code></pre> <p>And then you use it like this:</p> <pre><code>SearchResult sr = checkAddress("5556"); if (sr.isFound) { String name = sr.name; //... } </code></pre> <p>If you want, you can (and probably should) make the <code>final</code> immutable fields non-<code>public</code>, and use <code>public</code> getters instead.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload