Is CONSTANT.equals(VARIABLE) faster than VARIABLE.equals(CONSTANT)?

后端 未结 7 1714
小蘑菇
小蘑菇 2021-02-07 11:43

I had an interesting conversation with one of my team mate.

Is CONSTANT.equals(VARIABLE) faster than VARIABLE.equals(CONSTANT)in Java?

7条回答
  •  臣服心动
    2021-02-07 12:11

    One good comparison can be:

    private static String EXAMPLE = "Example";
    private String obj = null;
    

    case 1:

    if(obj.equals(EXAMPLE) {
    }
    

    This is throw null pointer exception..

    case 2 :

    if(EXAMPLE.equals(obj)) {
    }
    

    This will not throw null pointer exception..

提交回复
热议问题