I'm trying to understand is there any difference (performance/best programming practice..etc) between using a direct string vs creating a temporary variable in side a method and used it exactly once. Here is an example
Class Sample {
public boolean compareString(String str){
String test = "Test";
return test.equalsIgnoreCase(str);
vs
return "Test".equalsIgnoreCase(str);
}
}
In my opinion, both are the same. Instead of creating a variable, one can directly use it. Please let me if there are any other differences and which one is preferred?
testhad a chance of not being initialized.test.equalsIgnoreCase(str)will throw aNullPointer, while the other way will not.