简体   繁体   English

这两个条件的区别?

[英]Difference between these two conditions?

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? 这两个条件有什么区别?

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 . 许多程序员使用第二种方式只是为了确保它们没有得到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");
    }

Second one does not throw NullPointerException . 第二个不会抛出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 但同样它被认为是错误的代码,因为它可能发生strnull并且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. 鉴于选择更喜欢1因为它可以帮助您在早期阶段找到程序中的错误。
  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 否则,如果strnull则添加检查为null ,那么您将能够确定字符串是否真的不相等或第二个字符串不存在

     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");
    }

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. http://www.javaworld.com/community/node/1006 Equal方法比较两个字符串对象的内容。 So in your first case it compares str variable with "test" and in second it compares "test" to str variable. 因此,在第一种情况下,它将str变量与“test”进行比较,然后在第二种情况下将“test”与str变量进行比较。

The first if -statement test, whether str equals "test" . 第一个if -statement测试, str是否等于"test" The second if -statement test, whether "test" equals str . 第二个if -statement测试, "test"是否等于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" . 因此,这两个if -statements之间的区别在于,在第一个中,您使用参数"test"str对象发送消息。 then the str object compares, whether it equals to the argument and return true or false . 然后str对象进行比较,它是否等于参数并返回truefalse The second if -statement send a message to "test" . 第二个if -statement向"test"发送消息。 "test" is also a string object. "test"也是一个字符串对象。 So now "test" compares, whether it equals to str and return true or false . 所以现在"test"比较,它是否等于str并返回truefalse

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. 唯一的区别是第一个示例使用字符串对象“str”的equal()方法和“test”-string作为参数,而第二个示例使用字符串“text”的equal()方法和“str”作为参数。

The second variant can't throw a NullPointerException since its obviously not null. 第二个变体不能抛出NullPointerException,因为它显然不是null。

当您尝试首先修复静态字符串时,可以避免在许多情况下出现nullpointer异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM