Which Java string value is preferred to return: blank or null? I came across this snippet and am wondering if I should return blank in this case.

Which is more practical to return: blank or null?

public String getInfo() {
    String result = "";
    String uri = clientConfiguration.getUri();
    String regex = "http://(.*)/consolidate";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(uri);
    if (matcher.find()) {
        result = matcher.group(1);
    }
    return result;
}


Edit: Should I change it to:

 if(matcher.find()) { 
   return matcher.group(1); 
 } 
 return null;
The general question you ask "Which is better, "" or null?" is off-topic for CodeReview, but your code snippet has a number of reviewable items....

Regex Usage

Compiled Pattern performance

  String regex = "http://(.*)/consolidate";
  Pattern pattern = Pattern.compile(regex);
  Matcher matcher = pattern.matcher(uri);
  if(matcher.find()){
      result = matcher.group(1);
  }


This snippet makes it look like you know what you are doing, with the Pattern compile, etc. But, there is no performance benefit in the way you have done this. Compiled Java Pattern instances are thread-safe, and compiling them for one-time-use is not useful. It is common practice to make the Pattern a static-final field:

private static final Pattern MYPATTERN = Pattern.compile("http://(.*)/consolidate");


Then you can re-use that compiled pattern as much as you like, in any method, in any thread, like:

  Matcher matcher = MYPATTERN.matcher(uri);
  if(matcher.find()){
      result = matcher.group(1);
  }


find vs. matches

Now, matcher.find() and matcher.matches() are different methods.


find() will scan the input looking for any point inside the input where the pattern will match....
matches() does just one scan, and it matches the entire input string against the entire pattern.


With the input junkhttp://a/consolidate/junk:


find() will find your pattern
matches() will not find your pattern


The pattern

Now, as for the actual Regex... it appears that you want your pattern to match the HTTP 'host' against which you have the 'consolidate' path.... but, your pattern will match a lot of things which I would consider to be unexpected... for example, your pattern will return the following:

http://myhost/consolidate   from  http://http://myhost/consolidate/consolidate
myhost/consolidate          from  http://myhost/consolidate/consolidate
myhost:8080                 from  http://myhost:8080/consolidate
                            from  http:///consolidate


Each of the above input values will produce unexpected results.

Correct Regex

There is not a correct regex for matching URL's.... even if a regex appears that it will match, it is still not the correct solution ;-)

Solution

Use the java.net.URI class to validate your input. I have an example here, where I choose to return null if there is no configured URI, or throw an exception if there is a configured URI and that URI is not a valid value. This would be 'sensible' for many configurations, I expect.

public String getInfo() {
      String urival = clientConfiguration.getUri();
      if (urival == null || urival.isEmpty()) {
          return null;
      }
      try {
          URI uri = new URI(urival);
          return uri.getHost();
      } catch (URISyntaxException e) {
          throw new IllegalStateException("The configured URI is not valid: " + e.getMessage(), e);
      }
}