The difference between a Java hollow string and a null string

Source: Internet
Author: User

For an empty string, this is an object. He was instantiated by the object "", except that his 0-character-length content is empty.

The string variable can also store a special value, which is null, which is not associated with the other object and the variable. It's almost like an object that's not instantiated, it just defines an object.

Code testing

Package Study;public class MyString {public static void main (string[] args) {String str1=null; String str2= ""; System.out.println ("str1" +str1); System.out.println ("Str1" +str1.length ());/* * str1nullexception in thread "main" Java.lang.NullPointerExceptionat Study.myString.main (mystring.java:8) b error due to the absence of an object instantiation null pointer exception *  */system.out.println ("str2" +str2); System.out.println ("Str2" +str2.length ());}}

See that the null pointer exception is not instantiated because it is being reported.

The difference between a Java hollow string and a null string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.