if(null check)-else vs try catch(NullPointerException) which is more efficient?

后端 未结 6 1744
春和景丽
春和景丽 2020-12-28 17:48

Which of the following three functions is more efficient;

  public String getmConnectedDeviceName1() {
        if(null != mServerDevice) {
            retur         


        
6条回答
  •  萌比男神i
    2020-12-28 18:11

    Using try-catch clause definitely not a better way.

    Also try-catch results in instantiating of new object (the exception). And also if else improves the readability. Thus I'd say that (n!=null) is faster in case you have a lot of cases where n == null. Also n!=null is superfast construct.

提交回复
热议问题