The difference between String s; and String s=null; and String s="a" and String s=""

1、

Q: What is the difference between null and ""

String s=null;
s.trim()// will throw a null pointer exception
String s="";
s.trim();//No exception

answer:

null means that an empty object is declared, which means that no memory resources are applied for, that is, a reference variable is declared and the reference is initialized, but the reference does not point to any object. But you can pass it as a parameter or use it in other ways, you can't call his method as an object, it's not a string at all;

"" means that an object instance is declared, and the value of this object instance is an empty string of length 0. This statement means declaring and referencing an object, except that the object is 0 bytes. So now that you have an object, you can call the method of the object;

"" represents the existence of a string, and its value is "";

null means that the string has no actual value at all and doesn't know what it is.

2、

ask:

String s;
String s=null;
String s="a";

What's the difference? For these three cases, when using out.println(s), the first one will be abnormal, the second one will output null, and the third one will output a.

answer:

The first statement just declares a reference variable, but does not initialize the reference, so any operation on the variable s (except initialization assignment) will throw an exception;

Both the second and the third define the String type variable s and assign it an initial value, but the second assigns the value to null (empty).

3、

ask:

String s="a";
String s=new String("a");

What's the difference?

answer:

The essential difference is that the former is to write a character 'a' in the string pool, and then use s to point to it; the latter is to create a null character object with the content of "a" on the heap.

String str="aaa";//Allocate memory on the stack
String str=new String("aaa");//Allocate memory on the heap

4、

ask:

A String a is declared; what is the difference between a=="" and a==null in subsequent judgments?

Answer: null is used to determine whether the reference type has allocated storage space, and "" is for strings. In actual processing, it is often considered that "" and null represent the same meaning, that is, they both represent no value.

Suggested syntax:

if(a==null||a==""){
}

5、

ask:

String abc=null;
String abc="";
String abc;

What is the difference between the three spellings?

answer:

1. Create an empty string object;

2. Create a string object with an empty string;

3. Declare a string object, but no memory is allocated, while 1,2 have allocated memory

For the last representation, it cannot be if(abc==null), or int length=abc.length(); when compiling, it will prompt that it may not be initialized;

6、

ask:

String abc=null;
String abc;
String a="";
String b="";
String b="";
String c=new String("");
String d=new String("");

answer:

1 is equal to 2. Unlike the C language, Java does not allow a dangling reference for security reasons, and the reference address that is not assigned is automatically assigned to null to prevent access to any memory;

In 3 and 4, variable a and variable b will point to the same memory address (the address of "");

In 5 and 6, the variables c and d will not point to the same address, but the addresses of two "" contents, and are different from a and b;

The String class maintains a string pool. For assignment methods such as 3 and 4, String will look in this pool to see if the string is already in the pool. If it is, it will directly point to the address;

If not, generate an instance into the pool and then point to that address. It can be seen that the method of referencing 3 and 4 for the same content string multiple times saves memory than the method of 5 and 6. The reason for this is that String is A quantity whose content is immutable, using the design pattern GOF.FLYWeight

7、

ask:

String s; under what circumstances can be equivalent to String s=null; and under what circumstances is it not equivalent?

answer:

public class StringTest{
   static String s;//*
   public static void main(String[] args){
   //String s;//**
   System.out.println(s);
}
}

Compile and run the above code, it will print null;

It can be seen that the mark marked with * is automatically initialized (s will be automatically initialized to null);

And if you uncomment the line marked with **, the code will not compile, because this line defines a local variable, and local variables are not automatically initialized;

This leads to the conclusion:

In the definition of member variables, String s; is equivalent to String s=null;

In the definition of local variables (method variables), Strings; is not equivalent to String s=null; at this time, the assignment that s must be displayed must be used.

As long as it is defined in the method, the initial value should be explicitly assigned, and the main() method is no exception, and the compiler will automatically assign the initial value outside the method.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324624702&siteId=291194637