Explanation of the difference between null and ""

Source: Internet
Author: User

String S1 = null;

S1.trim ();//This will throw a NPE (null pointer exception).


String s2 = "";

S2.trim ();//The NPE will not be thrown at this time.


The reasons are as follows:

The null representation declares an empty object, where S1 is not a string. An empty object cannot do anything except = and = =.

String S1 = null, which simply defines the handle, which is the reference, but the reference does not point to any space.


"" represents an instance of an object that is an empty string of length 0.

String s2 = ""; The reference already points to a piece of memory space that is an empty string, and is something that actually exists that can be manipulated.


Null can be assigned to any object, "" is not available.

Null and "" are two concepts, "" represents a string, its value is "", and null means that the string has no actual value;

Null is an empty object, "" is an empty string.

String s = null;//null is allocated for allocating heap memory space. S is a reference, he is not an object, if used is a null reference.

The String s;//allocates a memory space, but no objects are stored. No initialization.

string s = "";//allocates a memory space, which is stored in a string object.


String s;

The difference between s = = null and S = = "":

If S is not assigned a value, s = = "" will cause an exception,

When actually processed, null and "" represent the same meaning, all of which represent no value.

Frequently Used methods:

if (s = = NULL | | s = = "") {


}

S is null to directly return TRUE, no subsequent statements are executed, so no error is taken.

Null is usually used to determine whether the reference type is allocated storage space,

"" is for a string,

String is actually a string pointer and is also a reference type.

If you do not assign a value to S, which is initialization, using s = = "" will cause an exception.


String s in the member variable, equivalent to string s = null;

In a method variable, string s; not equivalent to string s =null;s must be explicitly assigned before it can be used.



Explanation of the difference between null and ""

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.