Which is faster, try catch or if-else in java (WRT performance)

前端 未结 12 1948
暖寄归人
暖寄归人 2020-12-05 02:22

Which one is faster:

Either this

try {
  n.foo();
} 
catch(NullPointerException ex) {
}

or

if (n != null) n.foo();         


        
12条回答
  •  既然无缘
    2020-12-05 02:42

    If n.foo() happens to throw internally a NPE, you are off for a long debugging session (or worse, your app fails in production..). Just don't do it.

    How many nano-seconds do you plan to save, anyways?

提交回复
热议问题