When falling back on default values (in the below case name "John Doe"), which code appears better from the readability and possibly performance point of view:
String name;
if(last != null && first != null) {
name = last + first;
} else {
name = "John Doe";
}
Or a case without else statement:
String name = "John Doe";
if(last != null && first != null) {
name = last + first;
}
Does any of the above approaches have any significant drawbacks in your opinion?
答案 0 :(得分:0)
My personal preference is the second option, because of two reasons:
In terms of performance, you need to measure it, but I bet the Java Virtual Machine is able to optimise both of the above approaches and there are no significant differences in execution.