Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 785473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:53:04+00:00 2026-05-14T20:53:04+00:00

Could someone please give me some sample code that uses an output parameter in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:53:05+00:00Added an answer on May 14, 2026 at 8:53 pm

    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

    • Does Java have something like C#’s ref and out keywords?
      ?
      (NO!)
    • Is Java pass by reference? (NO!)

    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

    • What is meant by immutable?
    • StringBuilder and StringBuffer in Java

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 487k
  • Answers 487k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You extend the parent object, which is initialized when you… May 16, 2026 at 8:13 am
  • Editorial Team
    Editorial Team added an answer Have a look at Asynchronous I/O. This will free up… May 16, 2026 at 8:13 am
  • Editorial Team
    Editorial Team added an answer You need to append it to a container, then call… May 16, 2026 at 8:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Related Questions

No related questions found

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.