-5

Lets say we have a string in java. Can we compare this string to "" using the ==? For example:

String myString = "";
if(myString == "");
jotasi
  • 4,797
  • 2
  • 25
  • 49

1 Answers1

1

Of course you can (insofar that compilation will pass), although you will probably not get the result you expect since using == will compare references not contents.

My favourite way is to use the Yoda Expression "".equals(myString) since then you don't need to pre-test myString for null.

Else you could use myString.isEmpty().

Bathsheba
  • 227,678
  • 33
  • 352
  • 470