-5

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

String myString = "";
if(myString == "");
jotasi
  • 5,077
  • 2
  • 29
  • 51

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
  • 231,907
  • 34
  • 361
  • 483