Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 1
    \$\begingroup\$ I don't really know Java that well but can't you just use s=="" \$\endgroup\$ Commented May 4, 2017 at 15:28
  • 2
    \$\begingroup\$ @JohanduToit In Java unfortunately not. String is an Object, and since Java is passed-by-reference, s and "" doesn't point to the same reference, even if they have the same value. So new String("test") == "test" returns false, and new String("test").equals("test") returns true. String literals are an exception to this rule, so "test" == "test" will return true, because the compiler references to the same spot in the background. Perhaps better explained here or .equals in general here \$\endgroup\$ Commented May 4, 2017 at 18:09
  • \$\begingroup\$ @JohanduToit You could do s.intern()=="" to force Java to replace it with the equivalent object from the string pool, but that's 14 bytes, not really an improvement over the other methods. \$\endgroup\$ Commented Feb 14, 2018 at 20:44
  • \$\begingroup\$ @KevinCruijssen [...] since Java is passed-by-reference [...] -- more importantly since Java does not allow operator overloading and defines equality as identity. \$\endgroup\$ Commented Sep 26, 2018 at 14:26
  • \$\begingroup\$ actually, interesting enough, you can use s=="" if you make s="' at first, see my fizz buzz java program for an example codegolf.stackexchange.com/questions/58615/1-2-fizz-4-buzz/… I love how my ide says that s=="" doesn't work, but I can run the program just fine \$\endgroup\$ Commented Feb 2, 2021 at 18:05