search

Home  >  Q&A  >  body text

Java中为什么查询空字符串跟判断是否为null的时候可以不用equals?

if(id!=null&&id!="")
代码中经常这样写,空字符串跟null 都不用equals吗?

大家讲道理大家讲道理2891 days ago538

reply all(8)I'll reply

  • 高洛峰

    高洛峰2017-04-17 17:53:37

    First of all, your id!="" is wrong. It should be written as !id.equals("").
    Secondly, you must understand the meaning of == and equalsid!=""这种写法是错误的,要写成!id.equals("")
    其次,要明白==equals的含义

    1. ==表示两个对象是同一个对象的引用,==null表示这个对象不是任何实例对象的引用,该对象为空。

    2. equals

      1. == means that the two objects are references to the same object, ==null means that this object is not a reference to any instance object, and the object is empty.
    3. 🎜equals is a method in the String class, indicating that the contents of the two strings are the same. 🎜🎜 🎜

      reply
      0
  • 怪我咯

    怪我咯2017-04-17 17:53:37

    It’s been written like this all the time, and I’m asking, why?

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 17:53:37

    In fact, you need to use equals for empty strings, otherwise an error will occur. null can be used!=

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:53:37

    The code below, the result is false

    String d = new String("");
    System.out.println(""==d);

    So you must use equals to judge the empty string, such as

    public static boolean isEmpty(String str) {
        return (str == null || "".equals(str));
    }

    Or use length to judge and expand to any CharSequence, such as

    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:53:37

    1. The equals() method exists in the Object class, and all wrapper classes such as Integer, Long, etc. This function has been re-implemented. If you write a class and use the equals() method in the class, the effect of this method is equivalent to "==", which will determine whether the object references of the two classes are the same, and when packaging equals in the class will determine whether the contents of the object are equal.
    2. If the judgment is written as if(id.equals("")&&id!=null), there will be a NullPoint problem. If the value of id is null, you can remove the call equals() method will throw an exception. equals()方法存在于Object类中,所有的包装类如IntegerLong等均对该函数进行了重新实现,假如你自己写了一个类在类中使用equals()方法,该方法的效果等同于"==",会判断两个类的对象引用是否相同,而在包装类中equals会判断该对象的内容是否相等。
    2.如果该判断写为if(id.equals("")&&id!=null),会存在NullPoint的问题,假如id的值为null,你去掉调用equals()方法将会抛出异常。

    3.

    解释一下以上程序 str1 == str2比较的是两者的引用是否相同,我们声明的字符串会被存放到字符串常量池中,str1时会将abc放入常量池,str3的时候会首先检查常量池中是否有字符串abc有的话就会直接引用。str2因为是直接new了一个新的对象,所以地址肯定与str1的不同。那为什么使用了intern方法之后就相同了呢
    当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串,则返回池中的字符串,此时并不会去生成新的字符串。因为还是引用了常量池中的字符串所以str2.intern() == str1返回值还是true

    3.🎜 🎜🎜 🎜Explain that the above program str1 == str2 compares whether the two references are the same. The string we declare will be stored in the string constant pool. When str1 will put abc into the constant pool. When str3 is used, it will first check whether there is a string abc in the constant pool. If so, it will be referenced directly. Because str2 directly creates a new object, the address must be different from str1. Then why is it the same after using the intern method?
    When the intern method is called, if the pool already contains a string equal to this String object, the pool is returned. The string in will not generate a new string at this time. Because the string in the constant pool is still referenced, the return value of str2.intern() == str1 is still true. 🎜 🎜That’s it. (●'◡'●)🎜

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:53:37

    id==null时,表示id这个引用没有指向任何对象,只是一个引用;Java中具体的对象才可以调用方法(这里就是equals())。所以当id==null时调用equals()是会抛出NullPointerException

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:53:37

    This code is wrongly written, it does not use equals to compare empty strings, and there are many empty strings, such as tab, carriage return, etc., there is no check. There are many ready-made functions that can be used, such as StingUtils.isBlank in apache commons Method, Guava's Strings.isNullOrEmpty method can all determine the situation of null and blank strings.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:53:37

    You still need to understand the difference between == and equals, the difference between "" and null,
    == is to compare whether the values ​​are equal or whether the addresses of reference types are equal
    equals is to compare the values ​​of reference type addresses, that is, whether the contents are equal

    reply
    0
  • Cancelreply