4

Sorry if my question is silly or not it doesnot matter. But i just want to know what will happen in these two conditions.

public class Test {
    public static void main(String[] args) 
    {
        String str="test";
        if(str.equals("test")){
            System.out.println("After");
        }
        if("test".equals(str)){
            System.out.println("Before");
        }
    }
}

Both are giving same results only. But i know there is some reasons.I dont know about that. What is difference between these two conditions?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Ami
  • 4,241
  • 6
  • 41
  • 75

6 Answers6

12

There are no difference between them at all. Many programmers use the 2nd way just to make sure that they don't get a NullPointerException. That's all.

    String str = null;

    if(str.equals("test")) {  // NullPointerException
        System.out.println("After");
    }
    if("test".equals(str)) {  // No Exception will be thrown. Will return false
        System.out.println("Before");
    }
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

Second one does not throw NullPointerException. But again it is considered as bad code because it might happen that str is null and you do not detect that bug at this point instead you detect it somewhere else

  1. Given a choice prefer 1 since it helps you to find bugs in the program at early stage.
  2. Else add check for null if str is null then you will be able to make out are strings really not equal or is second string does not present

    if(str == null){
    //Add a log that it is null otherwise it will create confusion that 
    // program is running correctly and still equals fails
    }
    if("test".equals(str)){
        System.out.println("Before");
    }
    

For first case

    if(str.equals("test")){//Generate NullPointerException if str is null
        System.out.println("After");
    }
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
2

Actually both are same. There is no difference between this two. http://www.javaworld.com/community/node/1006 Equal method compares content of two string object. So in your first case it compares str variable with "test" and in second it compares "test" to str variable.

Jemish Patel
  • 444
  • 3
  • 13
1

The first if-statement test, whether str equals "test". The second if-statement test, whether "test" equals str. So the difference between these two if-statements is, that in the first you send a message to the str object with the argument "test". then the str object compares, whether it equals to the argument and return true or false. The second if-statement send a message to "test". "test" is also a string object. So now "test" compares, whether it equals to str and return true or false.

asdf
  • 11
  • 1
1

They do pretty much the same.

The only difference is that the first example uses the equal() method of the string object "str" with the "test"-string as parameter while the second example uses the equal() method of the string "text" with "str" as parameter.

The second variant can't throw a NullPointerException since its obviously not null.

Anonymous
  • 11
  • 1
0

when you try first fixing static string you can avoid nullpointer exception in many situations.

sunleo
  • 10,589
  • 35
  • 116
  • 196