1

I have this C# static function below that takes a MatchCollection, an input string and a pattern to search for. The MathCollection is passed by reference so that to use it I don't have to run a match twice. I can use it thusly:

      MatchCollection matches = new MatcchCollection();
      string pattern = "foo";
      string input = "foobar";

      if (tryRegMatch(matches, input, pattern) { //do something here}

 public static boolean tryRegMatch(out MatchCollection match, string input, string pattern)
    {   
        match = Regex.Matches(input, pattern);
        return (match.Count > 0);
    }

The question is whether it is possible to do this in Java I have read several articles that state Java is pass by value (keeping it simple). By default C# is, but you can use the 'out' modifier to make it pass by reference. I am doing a lot of matching, and this would make the coding simpler, otherwise I have to run the match then test it separately for success.

David Green
  • 1,210
  • 22
  • 38
  • 1
    Java only knows how to pass by value, never by reference; so no, it is not possible _stricto sensu_ – fge Oct 19 '14 at 01:53

1 Answers1

1

No. But you can approximate a reference idiom by simply wrapping the object.

class MatchRef
{
    public MatchCollection match;
}

public static boolean tryRegMatch(MatchRef matchRef, string input, string pattern)
{   
    matchRef.match = Regex.Matches(input, pattern);
    return (matchRef.match.Count > 0);
}
codenheim
  • 20,467
  • 1
  • 59
  • 80