Suppose I have to compare two Strings:
public String checkComaprision(String str) {
if (!("Hello".equals(str))) {
System.out.println("String didn't match to hello");
}
if ("".equals(str) || null == str || (!str.equals("Hello"))) {
System.out.println("String didn't match to hello");
}
}
Here I have used two methods of string comparison insider string. I have read at many places where we compare string with "" and with null before actual comparison. But I think the first case will work properly, and if so, then why should I check for null and blank?
Is there any case where the first comparison will fail? Which is the better approach?
nullcheck.