Generally, null in java refers to empty. But please note that there is a difference between the empty string "" and null.
"" means there is a string, but the string content is empty and the length is 0; null means the object is empty.
If you use a null object to call a method, a null pointer exception will occur.
So, when comparing string contents, you must use a non-empty string to call the comparison method.
The reference code is as follows:
public class Test { public static void main(String[] args) { String str1 = null; String str2 = ""; System.out.println(str1==str2);//false System.out.println(str2.equals(str1));//false System.out.println(str1.equals(str2));//空指针异常java.lang.NullPointerException } }
PHP Chinese website has a large number of free JAVA introductory tutorials, everyone is welcome to learn!
The above is the detailed content of What are the differences between java null and empty. For more information, please follow other related articles on the PHP Chinese website!