Home> Java> javaTutorial> body text

What are the differences between java null and empty

爱喝马黛茶的安东尼
Release: 2019-12-27 13:44:07
Original
3266 people have browsed it

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 } }
Copy after login

PHP Chinese website has a large number of freeJAVA 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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!