Empty object (NULL), null (empty), space in Java about string

Source: Internet
Author: User

Source: Empty object (NULL), null (empty), space in Java about string

Defined

Empty object:
String s = null;

An empty object is defined as an object s, but no space is allocated to the object, i.e.the object is not instantiated, therefore, the empty objectthrows an exception when all object methods are called.such as S.length (), S.isempty () and other methods.

Null value:
String k = "";

A null value refers to a character bed object that has been instantiated, that is, the system has allocated space for the variable, justthe contents of the object are empty。

Space:
String n = "";

Refers to a character object that has been instantiated,the contents of the object are spaces。



Judge
For the above three cases, how to determine that a character variable belongs to the above type, the following main methods are described below:

Null object judgment:
Determines whether a character variable is null, using logical judgment equals(= =) with null objectcomparison, the code expression is as follows:

s = = null; (really null)

Note: Empty objectsobject methods such as equals (), isEmpty () cannot be used because it is not instantiated.

Null-valued Judgment:

There are three ways to judge a null value:
(1) The Equals method compares the contents of the two objects, and can be used to express statements: K.equals ("");
(2) The length () method is to look at the number of characters of a string object, which can be judged by an expression: k.length () = = 0;

(3)IsEmpty ()The method is to determine the number of characters in a string object is 0, the expression can be judged: K.isempty ();

For example, if you decide that the queue has created an object, you do not have null. Again to determine if the object is zero , so use IsEmpty ()



Space to judge:
The space content is a space, although in the output display time and empty value, is empty, but in the system inside the memory mechanism is different, the space string indicates that the object's memory space contains content, is a space, the number of characters is 1, therefore, in the use of the following methods the result is as follows:

The output value of N.length () is 1, and the N.isempty () output is false.

Judgment statement: n.equals ("");


(RPM) null object (NULL) in Java about string, 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.