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