Lets say we have a string in java. Can we compare this string to ""
using the ==
?
For example:
String myString = "";
if(myString == "");
Lets say we have a string in java. Can we compare this string to ""
using the ==
?
For example:
String myString = "";
if(myString == "");
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()
.