Java empty string "" and null Difference __java

Source: Internet
Author: User

empty string ""

1, type:"" is an empty string (String), the length of 0, accounting for memory , in memory allocated a space , you can use object The method in. (For example: "". toString (), etc.)

2. Memory allocation: Declares a reference to a string type whose value is the empty string, which points to the str1 memory space.

NULL

1, type: null is a null reference , representing the value of an object , there is no memory allocated , the method of calling null string will throw a null pointer exception . (For example: Str1.endswith (STR2); Java.lang.NullPointerException)

2. Memory allocation: Represents a reference to declaring a string object, but points to null, which means that no memory space is pointed.

Example: String str1 = ""; STR1 corresponds to an empty string that declares the object's reference
String str2 = null; STR2 reference is empty

String str3 = new string (); STR3 will point to a specific string instance with the default value ""

Note: str1 and STR3 are instantiated, and str2 are not instantiated, but str1 and STR3 refer to different addresses, but the same values are null.


NULL is not an object (a null reference), "" is an object, so the comparison should be if (Str1.equals ("")) and if (str2 = = null), or the object is compared with Equals (), and Null is compared with the equals sign. The correct writing is to judge whether it is an object, and if so, to judge whether it is an empty string.


Supplemental NOTE: 1, Java class objects are used to access the handle, similar to the pointer in C.

2, in Java, there are two methods for determining equality: "= =" and the Equals () method, which is based on the address, and only the address and value are equal, and the two variables (reference types) are equal, and the value of the variable is equal as long as the value is equal.

3, in Java, variables and reference variables are in the stack (stack), and objects (new out) are stored in the heap (heap).

For example: string str = new String ("abc"),//str is stored in stack, the value of ABC is stored in heap and points to its allocated memory space.

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.