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

前端 未结 12 1936
暖寄归人
暖寄归人 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:48

    This issue has discussed recently by Dr. Heinz:

    http://javaspecialists.eu/webinars/recordings/if-else-npe-teaser.mov

提交回复
热议问题