What is faster if statement or set?

时间:2016-04-15 15:04:13

标签: java if-statement setter

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.

with if statement

 if (foo.getName() != null) {
         bar.setFoo(foo.getName());
    }

simple

bar.setFoo(foo.getName()); //getName() can be null

2 个答案:

答案 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