Return empty String for null string - Java java.lang

Java examples for java.lang:String Null Or Empty

Description

Return empty String for null string

Demo Code

public class Main {

  public static void main(String[] argv) {
    String s = "java2s.com";
    System.out.println(getString(s));
    /*  w  w  w .  j a  va2  s. co  m*/
    s = null;
    System.out.println(getString(s));
  }

  public static String getString(String s) {
    return s == null ? "" : s;
  }

}

Related Tutorials