1 2 3 Previous Next 39 Replies Latest reply: Aug 23, 2007 9:49 AM by 807605 RSS

    Pass String to the function by reference

    807605
      I need to write a function which parse a string and wrtites a result to 2 another Strings.
      Such as
      void parse(String src, String str1, String Str2) {
      //parsing src
      ...
      //end of parsing
      str1 = "output1";
      str2 = "output2";
      }

      But String are immutable. In this case I'm not able to do such function :(
      What is the best way to resolbe this problem?
        • 1. Re: Pass String to the function by reference
          807605
          Can't be done. Java passes everything - yes everything - by value, always. Why don't you just have the parsed string returned from this method?
          • 2. Re: Pass String to the function by reference
            807605
            You may use
            public class ParseResult
            { String output1;
              String output2;
              // constructor, getters and setters go here
            }
            With such a beast you have a couple of options:

            1.
            void parse (String str, ParseResult res)
            {
              // ...
              res.setOutput1("output1");
              res.setOutput2("output2");
            }
            2.
            ParseResult parse (String str)
            {
              // ...
              return new ParseResult("output1", "output2");
            }
            I like #2 better, it's more Java.

            A third option without a homegrown class:
            void parse(String src, StringBuilder str1, StringBuilder str2) {
              // ...
              str1.append("output1");
              str2.append("output2");
            }
            Message was edited by:
            OleVV
            • 3. Re: Pass String to the function by reference
              807605
              Solution with ParseResult is not good.
              This is the function call:
              ParseResult tmp = new ParseResult ();
              parse(src, tmp);
              str1 = tmp.getOutput1();
              str2 = tmp.getOutput2();
              So 4 lines of code instead of 1 line must be written for each function call :(
              • 4. Re: Pass String to the function by reference
                807605
                Can't be done. Java passes everything - yes
                everything - by value, always. Why don't you
                just have the parsed string returned from this method?
                I need to reutn 2 Strings but not 1. This is the main problem.
                • 5. Re: Pass String to the function by reference
                  807605
                  Solution with ParseResult is not good.
                  This is the function call:
                  ParseResult tmp = new ParseResult ();
                  parse(src, tmp);
                  str1 = tmp.getOutput1();
                  str2 = tmp.getOutput2();
                  So 4 lines of code instead of 1 line must be written
                  for each function call :(
                  So don't do that. Why not just have the parse method return the new String?
                  • 6. Re: Pass String to the function by reference
                    807605
                    Solution with ParseResult is not good.
                    This is the function call:
                    > ParseResult tmp = new ParseResult ();
                    
                    parse(src, tmp);
                    str1 = tmp.getOutput1();
                    str2 = tmp.getOutput2();> So 4 lines of code instead of 1 line must be written
                    for each function call :(
                    That is not really correct, as even with your approach, the two string variables should have been declared. Something like:
                    String str1 = null;
                    String str2 = null;
                    parse(src, str1, str2);
                    doSomething(str1, str2);
                    And with second approach from OleVV, you can make it with 2 lines instead of 4 :
                    ParseResult tmp = parse(src);
                    doSomething(tmp.output1, tpm.output2);
                    • 7. Re: Pass String to the function by reference
                      807605
                      So don't do that. Why not just have the parse method
                      return the new String?
                      I need to return 2 Strings, but not 1.
                      • 8. Re: Pass String to the function by reference
                        807605
                        So don't do that. Why not just have the parse
                        method
                        return the new String?
                        I need to return 2 Strings, but not 1.
                        So as suggested, define a class which carries 2 strings, and return that. Don't bother making the fields private and putting getter methods in, that's pointless, and nothing to do with encapsulation contrary to popular belief. A constructor might be useful, though
                        public class TwoStrings {
                          public String first;
                          public String second;
                        }
                        • 9. Re: Pass String to the function by reference
                          807605
                          So don't do that. Why not just have the parse
                          method
                          return the new String?
                          I need to return 2 Strings, but not 1.
                          In other words you need to reread solution #2 from reply #2.
                          • 10. Re: Pass String to the function by reference
                            796447
                            Nothing is going to satisfy the OP until Java has pass-by-reference.
                            We'll get right on that. The next version will be named Java OBARMIN in your honor.
                            • 11. Re: Pass String to the function by reference
                              807605
                              Nothing is going to satisfy the OP until Java has
                              pass-by-reference.
                              We'll get right on that. The next version will be
                              named Java OBARMIN in your honor.
                              Rubbish. Everyone knows the next version is going to be called fo'jizzle (search for it)
                              • 12. Re: Pass String to the function by reference
                                807605
                                who needs to search when one can read from the java site:


                                in the next version of java, codenamed fo' jizzle, they're going to introduce a new feature called drive-by reference whereby objects on the heap and references in scope will be randomly matched up to each other by an undisclosed and non-deterministic algorithm, in a big car and baseball cap, known as Pimp Daddy Heap. this is so that any old skool developers who think java is getting a little safe and boring can now convince themselves they're "keepin' it real J-side". the downside, of course, being that for us confused uncles, we'll be forever at the mercy of ClassCastExceptions ( to be renamed "DatShitDontFitException" ) when our references keep pointing to the wrong things, and NullPointerExceptions ( or "FoolDoneGoneLikeElvisExceptions" ) when there aren't enough objects to go round
                                • 13. Re: Pass String to the function by reference
                                  807605
                                  warnerja, Read the Java Manual. Objects are passed by reference in Java. Only the immutable objects (strings for example) are excluded from this rule.

                                  It looks like that the solution with the String wrapper is the best way to solve this problem. But this is a workaround but not solution :(
                                  • 14. Re: Pass String to the function by reference
                                    807605
                                    Heh, warnerja's 20000 posts against your 6 posts should already make you think before you post.

                                    Object references are passed by value, nothing is ever passed by reference in Java.
                                    1 2 3 Previous Next