0
public static String[] myfunc(String s){
    if (s == null) return null;
    if (s == "") return new String[0];
    //code.. 
    //this code will not run if s == "" 
}

I intended s == "" to cover all the empty strings.
But there seems to be cases where s != "" but s.length() == 0
What is the difference between s == "" and s.length() == 0?

jamric
  • 1
  • 1
  • 3
    The problem is that `s == ""` doesn't cover all empty strings (for example not those derived using substring, or using `new String(...)`, etc. – Mark Rotteveel May 25 '20 at 10:02
  • 1
    Besides that, instead of `s.length() == 0` or `s.equals("")`, you can simply use `s.isEmpty()`. – Holger May 25 '20 at 10:12

0 Answers0