Difference between various string comparisons in Java
Is there a difference between them?
if(myString.equals("")){
}
if(myString.equals(null)){
}
if(myString == ""){
}
I have a string, I don't know if it is empty or has multiple emtpy spaces. I just don't want it to be written to the database if it is invalid (if it is empty or contains spaces).
a source to share
Each of your examples is different from each other.
- The first one is a simple comparison example to see if it is
myString
empty (a string with no characters in it) - The second is always either false or
NullPointerException
- The third checks if the variable refers to an
myString
empty string constant. This will be true if the variable is explicitly initialized from an empty string constant, or if the variable is initialized with some "generated" empty string (for examplenew StringBuilder().toString()
) and then explicitly added to the constant pool withintern()
.
As pointed out in the comment, the first example can also be selected NullPointerException
, but not the third.
a source to share
Yes there is.
It will return true first if your string is empty.
The second will always return false.
The third returns true if your string is defined exactly like this:
String myString = "";
Note that this is not the same as case 1! The first tests are for equality of values, and the second for identity.
String myString = "";
String myOtherString = new String(myString);
assert myString == "";
assert myOtherString.equals("");
assert myOtherString != "";
Bottom line:
- use the first case to compare strings,
- use
myString == null
for null tests, - never use the third!
a source to share
pajton added the correct answer. to figure out what's wrong with your attempts:
//true if myString is the empty string. will fail on " "
if(myString.equals("")){
}
//always false, or fails
//if myString is null, you'll get a NullPointerException
//if it isn't, you'll get false
if(myString.equals(null)){
}
//a bad way to do things with unexpected results.
//when dealing with objects, == is true when each are the same object.
//two different objects that have the same content or value (e.g. clones)
//will return false using this technique
if(myString == ""){
}
a source to share
If you don't depend on external librairies, you can use the Apache commons StringUtils class, which provides a method called isNotBlank (), which seems to be perfect for your need:
Checks if a String is not empty (""), not null and not whitespace only.
StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true
a source to share
A string in Java is just another object. Your first comparison is the most accurate. It compares myString to a space and returns true if your string is also empty ("). The second comparison is always wrong. Third, it compares the equality of objects (think of it as comparing the address of an object). I think it will always return false if your string is not myString = "" (not sure about this).
a source to share
They are all different. Assuming myString is not null,
- Returns true if the string referenced by myString represents an empty string
- Will always return false
- Will return true if myString refers to the same String instance as "". This may be true in your program, but it may also be false.
a source to share
Just use Apache Commons StringUtils.isBlank ()
EDIT: To achieve what you want with @ zim2001's method, do this:
if ( StringUtils.isNotBlank(myString) ) {
// write to database
}
This is much easier to read than the current accepted answer. (But please see other posts here for how to work with String.)
a source to share