The difference between null and "" in Java

Source: Internet
Author: User
Tags gettext

Question one:

The difference between null and ""

String S=null;

S.trim () will throw an empty exception

String s= "";

S.trim () will not throw, why?

For:

A null representation declares an empty object and is not a string at all.

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

Null represents an empty object that does nothing for an empty object except = and = =; is a string, but there is no content in the string.

String S=null; just defines a handle, which means you have a reference, but this reference does not point to any memory space (only the stack memory is allocated and not allocated to memory ).

String S= ""; This reference is already pointing to the memory space of an empty string, which is an actual thing, so you can manipulate it without worrying about it.

Here "" and Null are definitely two concepts.

"" means a string exists, and its value is ""; null means the string has no actual value at all, and you don't know what it is ...

It means string string = null and string string;

Null is an empty object "" is an empty string

String S=null;//null is unallocated heap memory space but allocates stack memory space and initializes it to an empty object.

String a;//allocates a stack of memory space and does not deposit any objects

String A= "";//allocate a memory space, save a string object

Question two:

string s; and string s=null; and strings= "a"; what's the difference?

For these three cases, use OUT.PRINTLN (s), the first one will appear an exception, the second will output null. The third one will output a.

What is this for? What do you do with these three statements?

For:

The first one simply defines a string variable s, and does not give it an initial value, in Java, the default is to give it an initial value (to reduce the risk) when using a variable.

The second and third define the string type variable s and give it an initial value, except the second one gives null (NULL).

The main understanding is that string s;s is a reference ~ ~ It is not an object

The first is a reference with no initialization;

The second is a null reference;

The third is to write a character ' a ' in the string pool, and then use S to point to it.

Other than that

String s= "A" and string s=newstring ("a"); there is a fundamental difference.

The former is to write a character ' a ' in the string pool, and then use S to point to it;

The latter is a string object that creates a "a" content on the heap.

String str= "AAA"; allocating memory on the stack

String Str=new string ("AAA"); allocating memory on the heap

Strings; The system will automatically assign a value of NULL

Strings; Just allocating a memory space to s

Strings=null; is a null value stored in the allocated space

Strings= "a"; I don't have to tell you that. The value of the allocated space is the character a

Question three:

Declares a stringa; a variable.

What is the difference between a== "" and a==null in future judgments?

For:

If you do not assign a value to a, a== "" causes an exception.

In practice, it is often assumed that "" and null represent the same meaning that all represent no value.

In this case, the following syntax is recommended:

if (A==null | | a== "")

{

}

If A is null, no subsequent judgment is performed and returns true directly.

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

"" is for a string;

The string type is actually a string pointer, which is also a reference type

So if you don't assign a value to a, a== "" will cause an exception

So if (A==null | | a== "") {} This is also the correct wording

##########################################################

If no information is entered in a text box, the string object obtained by GetText () of the text box object is null;

If the information in a text box is not entered as a space, then the string object obtained by GetText () of the text box object is "";

###########################################################################

Question four:

String Abc=null; String abc= ""; STRINGABC; What is the difference between three ways of writing?

For:

1: Create an empty string object,

2: Creates a string object that is empty.

For the last expression, you cannot have if (abc==null), or int length = Abc.length (), and the compilation will indicate that it may not be initialized.

String Abc=null;

String abc= "";

It is generally recommended to use the second type

The first kind of ABC points to NULL, many times to determine whether the string is empty, it is easy to miss this situation, when invoking the related method of string error

The second is relatively simple, the method of string can be used, the judgment of the time will not be wrong

1) String Abc=null;

2) String ABC;

3) String a= "";

4) String b= "";

5) String c=newstring ("");

6) String d=newstring ("");

1) is equal to 2), and C language, Java for security reasons do not allow a hanging reference, no assignment of the reference address is automatically assigned to NULL to prevent access to any memory

3) and 4), the variables A and B will point to the same memory address (the "" address)

5) and 6), the variables C and D do not point to the same address, but the address of the two "" content, and, unlike A, B, in fact, 3) and 4) are equivalent to NewString (""). Intern ().

The string class maintains a string pool, for an assignment method such as 3 and 4), where string looks for strings that are already in the pool, and if so, points directly to that address.

If not, generate an instance into the pool and point to that address, visible for the same content string multiple references when 3) 4) method than 5) 6) method left memory, the reason for doing so is

Because string is an immutable amount of content, the use of the design pattern gof.flyweight;

But there is a key point, no one said, this is:

string s; Under what circumstances can be equated to string s=null; and under what circumstances is not equal?!

Consider the following code:

Stringtest.java

public class Stringtest {

static String s; //*

public static void Main (string[] args) {

String s; //**

System.out.println (s);

}

}

Compile and run the above code, and the null will be printed.

The lines marked with an * number are automatically initialized (s is automatically initialized to null).

If you uncomment a line marked with a * * number, the code cannot be compiled because the row defines a local variable, and the local variable is not automatically initialized.

This concludes:

In the definition of a member variable, String s; equals to Strings=null;

In the definition of a local variable (method variable), Strings is not equivalent to string s=null, at which point the S must be explicitly assigned.

These are small knowledge points, but in practical application is very important, it is easy to be ignored by some people, hereby proposed.

Another point to note is:

The main () method is no exception, and the compiler automatically assigns an initial value outside the method, as long as the variable is defined in the method.

The difference between null and "" in Java

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.