I have seen most cases developers use first string and after that variable used in .equal operation. What is the reason?
I would not use ("string").equals(var), since it may show that the programmer does not know if var might be null or not. I would document if a var can be null or not and check it.
Example:
String makeAString(@NonNull String aStringValue) {
Validate.notNull(aStringValue, "aStringValue must not be null");
if (aStringValue.equals("abcde") {
return "fg";
} else {
return "de";
}
}
String makeAString(@Nullable String aStringValue) {
if (aStringValue == null) {
return "kl";
}
if (aStringValue.equals("abcde") {
return "fg";
} else {
return "de";
}
}