if(id!=null&&id!="")
代码中经常这样写,空字符串跟null 都不用equals吗?
高洛峰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 equals
id!=""
这种写法是错误的,要写成!id.equals("")
。
其次,要明白==
与equals
的含义
==
表示两个对象是同一个对象的引用,==null
表示这个对象不是任何实例对象的引用,该对象为空。
equals
==
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. equals
is a method in the String class, indicating that the contents of the two strings are the same. 🎜🎜
🎜怪我咯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!=
天蓬老师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;
}
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
类中,所有的包装类如Integer
,Long
等均对该函数进行了重新实现,假如你自己写了一个类在类中使用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.🎜 🎜
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?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. (●'◡'●)🎜ringa_lee2017-04-17 17:53:37
id==null
时,表示id这个引用没有指向任何对象,只是一个引用;Java中具体的对象才可以调用方法(这里就是equals()
)。所以当id==null
时调用equals()
是会抛出NullPointerException
。
天蓬老师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.
天蓬老师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