The string type is of type:
-
Once created, the string cannot be changed. Once created, the "ABC" string object can no longer be changed to "ABCD"
-
Improved access to strings: "Cache" technology is used in the program. So all strings enclosed in "double quotes" in Java are created in the string constant pool. The string constant pool is stored in the method area.
-
During program execution, if a string is used, for example: "ABC" then the program ambulance in the string constant pool to search for the string, if not found in the string constant pool to create a new "ABC" string, if found directly to use. (The string constant pool is a buffer, in order to improve the efficiency of the access string)
public class Stringtest01{public static void Main (string[] args) {//Create an "ABC" string object, the object's memory address, and let the S1 variable be saved. S1 is a reference, and S1 points to the "ABC" object. String S1 = "abc";//Can you make S1 point again? S1 is a local variable, S1 front is not final, so S1 can be pointed again. But the "def" string itself is immutable. S1 = "Def"; String s2 = "Hello"; Creates a new "Hello" string object in a string constant pool that is immutable string s3 = "Hello"; directly from the string constant pool. System.out.println (S2==S3); true//compares two strings for equality and cannot use "= =" string s4 = new String ("abc"); String S5 = new String ("abc"); System.out.println (S4==S5); false//compare two strings for consistency, you must use the Equals method provided by the string class. System.out.println (S4.equals (S5)); true//after the execution of the following program, 3 string objects are created in a string constant pool//"AAA" "BBB" "aaabbb" string s6 = "AAA"; String s7 = "BBB"; String s8 = s6 + s7;
This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1662456
String type in Java