Home >Java >javaTutorial >What are the differences between java null and empty

What are the differences between java null and empty

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-12-27 13:44:073412browse

What are the differences between java null and empty

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn