Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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

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

I suspect this to be a false statement. But I am trying to figure out what should be qualitative reasoning behind this?

I know in both the cases the performance will not differ to any kind of significant state. But it was a recommendation under BEST PRACTICES which is making me uncomfortable. That's the reason I am looking towards a good reasoning that I want to present with this case.

Please HELP

like image 711
Ashish Agarwal Avatar asked Jun 06 '12 08:06

Ashish Agarwal


4 Answers

Interesting question. Here is the test I wrote:

public class EqualsTest {
    public static String CONST = "const";
    public void constEqVar(String var) {
        CONST.equals(var);
    }
    public void varEqConst(String var) {
        var.equals(CONST);
    }
}

Then I compiled it using javac: javac EqualsTest.java and disassembled it using javap: javap -c EqualsTest.

Here is the relevant snippet of javap output:

public void constEqVar(java.lang.String);
  Code:
   0:   getstatic       #2; //Field CONST:Ljava/lang/String;
   3:   aload_1
   4:   invokevirtual   #3; //Method java/lang/String.equals:(Ljava/lang/Object;)Z
   7:   pop
   8:   return

public void varEqConst(java.lang.String);
  Code:
   0:   aload_1
   1:   getstatic       #2; //Field CONST:Ljava/lang/String;
   4:   invokevirtual   #3; //Method java/lang/String.equals:(Ljava/lang/Object;)Z
   7:   pop
   8:   return

As you can see the only difference between theses 2 methods is order of operations: getstatic and then aload_1 in first case and aload_1 + getstatic in second case.

Pretty obvious that the execution time should not depend on this order.

The only reason to prefer const.equals(var) rather than var.equals(const) is to avoid NullPointerException.

like image 123
AlexR Avatar answered Nov 20 '22 15:11

AlexR


For me its not a speed issue, its a relability issue.

e.g.

"Hello".equals(a); // will never throw a NPE
a.equals("Hello"); // can throw an NPE.

You may prefer it to blow up when a is null but usually I don't.

like image 22
Peter Lawrey Avatar answered Nov 20 '22 14:11

Peter Lawrey


That depends only on the implementation of the equals method. It could be quicker, it could be the slower and it could be the same... Often it is the same. Also it does not depend on the fact that one is a variable and the other a constant but on the content both objects.

One advantage of Constant.equals(variable) is that you can't have a NullPointerException on the .equals

like image 2
tibo Avatar answered Nov 20 '22 15:11

tibo


If we compare CONSTANT key(Left side of equals method) with any Object(Right side of equals method) then compiler can do check comparison and give the expected result, but if we do vice versa Object((Left side of equals method)) comparison with Constant key((Right side of equals method)) then your program might through the NULL POINTER EXCEPTION.

public static void main(String[] args) {
    String CONSTANT_KEY = "JAVA";
    String string = null;

    // CASE 1
    if (CONSTANT_KEY.equals(string)) {
        System.out.println("I am in if block");
    }

    // CASE 2   
    if (string.equals(string)) {
        System.out.println("I am in if block");
    }
}

In above code case 1 is always safe to compare the objects to avoid NULL POINTER EXCEPTION instead of case 2.

like image 2
Vinod Pattanshetti Avatar answered Nov 20 '22 15:11

Vinod Pattanshetti