The difference between these two conditions?

Sorry if my question is stupid or not, it doesn’t 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 give only the same results. But I know that there are some reasons. I do not know about that. What is the difference between these two conditions?

+4
source share
6 answers

There is no difference between them. Many programmers use the second method to make sure that they do not receive a NullPointerException . It'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"); } 
+12
source

The second does not throw a NullPointerException . But then again, this is considered bad code because it can happen that str is null and you do not detect that bug at this point instead you detect it somewhere else

  • Given the choice, prefer 1 , as it helps you find errors in the program at an early stage.
  • Re-add the check for null , if str is null , then you can see the lines that are really not equal or the second line is not represented

     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 the first case

  if(str.equals("test")){//Generate NullPointerException if str is null System.out.println("After"); } 
+2
source

In fact, both of them are the same. There is no difference between the two. http://www.javaworld.com/community/node/1006 An equal method compares the contents of two string objects. Thus, in your first case, it compares the str variable with “test”, and in the second, it compares “test” with the str variable.

+2
source

The first test is if -statement, regardless of whether str is "test" . The second test is if -statement, "test" is str . Thus, the difference between the two if states is that in the first you send a message to the str object with the argument "test" . then the str object is compared whether it is equal to the argument and returns true or false . The second if -statement will send a "test" message. "test" also a string object. So now, "test" compares whether it is str and returns true or false .

+1
source

They do almost the same thing.

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

The second option cannot throw a NullPointerException because it is clearly not null.

+1
source

when you try to set a static string first, you can avoid the nullpointer exception in many situations.

0
source

Source: https://habr.com/ru/post/1442277/


All Articles