I want to set a property to an other property that could be null. I wonder what is faster, to wrap it around an if
statement or simply just set the property without the check.
if (foo.getName() != null) {
bar.setFoo(foo.getName());
}
bar.setFoo(foo.getName()); //getName() can be null
答案 0 :(得分:0)
If you are going to setFoo() either way, you might as well just set it and skip the condition. I would use the condition if you wanted to take an action if !null and a different action if null.
答案 1 :(得分:0)
when foo.getName() != null
would return true
, the setter would be called, so you will have 2 commands that will be executed, however the second example will execute only 1 command. I think the second one is more performant