1. Use the New keyword string S1 = new String ("AB"); // 2. Use string constants to assign values directly String s2 = "abc"; 3. Use the "+" operator for string connections String s3 = "abc" + "D"; String S4 = s3 + 5; Abcd5 Constant Pool Concepts: The Java runtime maintains a string pool, also known as a string buffer. String The pool is used to store the various strings produced in the runtime, and the contents of the strings in the pool are not duplicated . The normal object does not exist in this buffer pool, and the object created only exists in the stack area of the method. The creation of a string object is very elaborate, the key is to understand its principle. Principle 1 : When using any way to create a string object s, the Java runtime (the running JVM) takes this s in the string pool to find out if there is a string object with the same content, and if not, creates a string s in the pool, otherwise it is not added in the pool. Principle 2 : In Java, whenever you use the New keyword to create an object, a new object will be created (in the heap area). Principle 3 : Creating a String object using either direct or string concatenation, only checks the string in the maintenance string pool and does not create one in the pool. However, the string object will never be created in the stack area again. principle 4 : Creating a String object with an expression that contains a variable will not only examine the maintenance of the string pool, but also create a string object in the stack area. Finally point to the object in the heap memory |