String object data types in Java are fully parsed

  • 2020-04-01 01:10:49
  • OfStack

1. First of all, String does not belong to the eight basic data types, but is an object.

Since the default value of the object is null, the default value of String is also null. But it's also a special kind of object, with some features that other objects don't have.

2. Both new String() and new String("") declare a new empty String.

3. The String STR = "kvill";

String STR = new String (" kvill "); The difference between:
Instead of talking about the heap or the stack, we'll simply introduce the simple concept of constant pools.
A constant pool is defined at compile time and is stored in the compiled. Some data in the class file. It includes constants in classes, methods, interfaces, and so on, as well as string constants.

See example 1:
 
String s0="kvill"; 
String s1="kvill"; 
String s2="kv" + "ill"; 
System.out.println( s0==s1 ); 
System.out.println( s0==s2 ); 

The result is:
True,
True,
First, we need to know that the result is that Java ensures that there is only one copy of a string constant.
Since the "kvill" in s0 and s1 in the example are string constants, they are determined at compile time, so s0==s1 is true; "Kv" and "ill" are also string constants. When a string is concatenated by several string constants, it must be a string constant itself. Therefore, s2 is also resolved as a string constant at compile time, so s2 is also a reference to "kvill" in the constant pool.
So we get s0==s1==s2;
Strings created with new String() are not constants and cannot be determined at compile time, so strings created with new String() do not go into the constant pool, they have their own address space.
See example 2:
 
String s0="kvill"; 
String s1=new String("kvill"); 
String s2="kv" + new String("ill"); 
System.out.println( s0==s1 ); 
System.out.println( s0==s2 ); 
System.out.println( s1==s2 ); 

The result is:
false
false
false
In example 2, s0 is still the application of "kvill" in the constant pool. S1 is a reference of the new object "kvill" created at runtime because it cannot be determined at compile time. And with that in mind, you can see why.

4. String. Intern () :

One more thing: in. The constant pool in the class file is loaded by the JVM at run time and can be expanded. The String intern() method is one way to extend the constant pool; When a String instance, STR, calls the intern() method, Java looks for whether there is a String constant of the same Unicode in the constant pool. If so, it returns a reference to it. Just look at example 3
Example 3:
 
String s0= "kvill"; 
String s1=new String("kvill"); 
String s2=new String("kvill"); 
System.out.println( s0==s1 ); 
System.out.println( "**********" ); 
s1.intern(); 
s2=s2.intern(); //Assign a reference to "kvill" in the constant pool to s2
System.out.println( s0==s1); 
System.out.println( s0==s1.intern() ); 
System.out.println( s0==s2 ); 

The result is:
false
* * * * * * * * * *
False // although s1.intern() is executed, its return value is not assigned to s1
True // indicates that s1.intern() returns a reference to "kvill" in the constant pool
True,
Finally, I have one more misunderstanding:
Someone said, "using string.intern () method can be a String class saved to a global String table, if you have the same value of the Unicode String already in this table, then this method returns the table for the address of the String, if do not have the same value in the table of strings, will own address registered to the table," if I had said the global String list as constant pool, his last words, "if in the String, and do not have the same value in the table will be its own address registered to the table" is wrong:
See example 4:
 
String s1=new String("kvill"); 
String s2=s1.intern(); 
System.out.println( s1==s1.intern() ); 
System.out.println( s1+" "+s2 ); 
System.out.println( s2==s1.intern() ); 

The result is:
false
Kvill kvill
True,
We don't have a "kvill" constant in this class, so we don't have a "kvill" constant in the constant pool at the beginning. When we call s1.intern(), we add a "kvill" constant in the constant pool.
S1 ==s1.intern() false indicates that the original "kvill" still exists;
S2 is now the address of "kvill" in the constant pool, so s2==s1.intern() is true.

5. Equals () and ==:

This is simply for strings to compare the Unicode sequences of two strings, and return true if they are equal; And == compares two strings to see if they have the same address, that is, whether they are references to the same string.

6. String is immutable

There's a lot to be said here, but once you know that an instance of a String is generated, it won't change, for example, String STR ="kv"+"ill"+" +"ans";
So we have 4 string constants, first "kv" and "ill" generate "kvill" in memory, and then "kvill" and "generate "kvill" in memory, and finally "kvill ans"; And assign the address of the String to STR, because the "immutable" of String generates a lot of temporary variables, which is why StringBuffer is recommended, because StringBuffer is mutable.

Related articles: