There is no doubt that the string class is one of the most important classes in Java. So it has many ways to understand and master.
Once a string has been assigned, the value cannot be changed:
Package cn.itcast_02;/* * String features: Once assigned, it cannot be changed. */public class Stringdemo {public static void main (string[] args) {string s = "Hello";//hello is staying in the string constant pool. After the assignment of Hello, it can't be changed. s + = "world"; System.out.println ("s:" + s); HelloWorld}} opens up three spaces throughout the process.
Look at the assignment process with a picture.
/* * String s = new string ("Hello") and string s = "Hello"; the former creates 2 objects (Hello creates an object in a constant pool, then creates it in heap memory (new), and then the new object points to the string constant pool , which creates 1 objects that directly point to a string constant pool. * = =: Comparing the reference type is the same as the address value * equals: Comparing the reference type by default is also the same as comparing the address value, and the string class overrides the Equals () method, comparing whether the content is the same. */public class StringDemo2 {public static void main (string[] args) {string S1 = new String ("Hello"); String s2 = "Hello"; System.out.println (S1 = = s2);//FalseSystem.out.println (s1.equals (S2));//True}}
/* * See Program Write Results */public class StringDemo3 {public static void main (string[] args) {String S1 = new String ("Hello"); String s2 = new string ("Hello"); System.out.println (S1 = = s2);//FalseSystem.out.println (s1.equals (S2));//truestring S3 = new String ("Hello"); String s4 = "Hello"; System.out.println (s3 = = S4);//FalseSystem.out.println (S3.equals (S4));//truestring S5 = "Hello"; String s6 = "Hello"; System.out.println (S5 = = s6);//TrueSystem.out.println (S5.equals (S6));//True}}
String in the end to create a few object problems, and then do in detail later in the topic.
"Getting Started with Java" the first quarter and the like (string strings cannot be changed once they are assigned)