The difference between an empty string ( "") and null and blank string ( "") of

1, type

null value is represented by an object, rather than a string.
E.g. declare an object reference, String A = null;
"" is represented by an empty string, that is to say its length is 0, it is a string.
For example, a statement string String str = "";

2, the memory allocation
String str = null; statement represents a string object reference, but the point is null, that is not yet pointing to any memory space;
String STR = ""; represents a string type declaration of reference, which value is "" empty string, str reference points to the memory space is the empty string;

"": Allocated memory space allocated to a
null: not allocate memory
"": allocating memory, allocating a space

 string str1 = "";    //空字符串      str1.length() 等于 0
 string str2 = null; //NULL
 string str3 = " ";   //空格串          str2.length() 等于 1
  • 1
  • 2
  • 3

In java variables and references is the presence of the stack (stack), and the object (new production) are stored in the heap (heap):

 

==============================================================================================

1, represents the difference

     string str1 = ""; // empty string str1.length () is equal to 0

     string str2 = null; //NULL

     string str3 = ""; // string space str2.length () is equal to 1

 

2, memory difference

      "": Allocating memory, allocating a space

     null: not allocate memory

      "": Memory allocation

 

3, using the difference

     Call null string method throws an exception.

     "" Is a character string (String) which is present in the memory it can use object in the Object. (E.g., "" .toString (); "" equals ().).

     It is a null empty object in memory does not exist. It can not use the object in the Object.

 

Remarks:

      Space: essentially the same as abc, it's a character. Its value (ASCII) 32

      '\ 0': indicates the end of the string, numeric (ASCII) is 0

 

 

 

Judgment of character in the process of development is the most normal thing, the problem is we could easily cause confusion in java, "" (empty string) and null, now be described:

For example, declare a String str;
      if str is null, then the memory did not create a string of like, str by reference.
      If str is an empty string, then the existence of a string referenced by str to like, but this value is the string ""
     null is used to indicate the absence of a reference example, and "" itself is an instance, have their own the object space, and "zzyyxx" this String no difference. There are two values! ! !
Secondly, Note:
     (1) when the value inside the oracle empty field, is read out from the oracle (when a program may be read out, to convert the object becomes empty string "null") the string "null".
     (2) When there are too URL parameters such as: oylin.jsp id = <% = id %>, id is null when the object is also possible to convert objects become empty string "null", in the writing process? time to pay more attention.
       Another problem is determination processing null or empty value String: There are two methods
       F (!! = Null && name.equals name ( "")) {
         // do something
         }

        or

       if (! "". equals ( name)) {// will be "" written on the front, so, regardless of whether the name is null, will not go wrong.
               do something //
         }

 

 

 


String to be interpreted string is empty, relatively simple, as long as the determination of the length of the String is 0 can, either directly by the method isEmpty () is determined.

    But many times we will put a number of invisible characters String also treated as an empty string (eg, space, tab, etc), this time not alone length or isEmpty () to determine, because technically up He said the String is not empty. This method can be used when String TRIM (), remove the leading blanks and trailing blanks guide, and then determines whether the air.

Example:

public class TestEmpty

   {
       public static void main(String[] args){
              String a = "       ";      
              // if (a.isEmpty())
             if (a.trim().isEmpty())
             {
                System.out.println("It is empty");
             }
            else 
            {
               System.out.println("It is not empty");
            }
       }
  }

The results: It is empty

 

 


Declare a String str

If str is null, then the memory did not create a string object str by reference.

If str is an empty string, it is determined that there is a string object referenced by str, only the value of the string is empty

null is used to indicate the absence of instances, and "" is an example of itself, with its own object space, and "123456" such strings there is no difference.

Second, we must pay attention to:

           1) When the inside of the field is empty oracle, the oracle is read out from the character string "null" (the program may be read out, to convert the object becomes empty string "null").

     (2) When there are too URL parameters such as: oylin.jsp id = <% = id %>, id is null when the object is also possible to convert objects become empty string "null", in the writing process? time to pay more attention.
Another problem is to determine the process control or null String: There are the following two methods
(name = null && name.equals ( "!!")) {
    DoSomething ();
}
, or
if (! "" Equals (name ). ) {// the "" EDITORIAL, regardless of whether the name is null, are not being given
   doSomething ();
}


----------------
Disclaimer: This article is the original article CSDN blogger 's Dawn ", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/sinat_34089391/article/details/80292377

Guess you like

Origin www.cnblogs.com/wfy680/p/11969701.html