[Java object-oriented] String, String Constant Pool, String initialization, intern method, common methods of String (interception)

Posted by smsulliva on Sun, 27 Feb 2022 03:30:51 +0100


Used in Java java.lang.String class represents string:

  • Before Java 9, the bottom layer used char [] to store character data
    Starting from Java 9, the bottom layer uses byte [] to store character data
  • All String literals (such as "abc") are instances of the String class
  • Once a String object is created, its character content cannot be modified

First look at a piece of code: and think about whether the output of this code is different from what you think.

public class Main {
	public static void main(String[] args) {
		String s = "555";
		s += "555";
		s = "666";
		System.out.println(s); // 666
	static void test(String str) {
		str += "555";

String s = "555";, s += "555";, s = "666" and other operations will open up a new memory space in the heap space, because string is an immutable string. Every time you want to modify the value of the variables (s, str) in the stack space, you can only change its pointer to point to the new memory in the heap space.

As we know, member variables and function parameters are opened in the stack space (because they may be destroyed at any time). In fact, s and str in the above code are not in the same layout in the stack space. The test method modifies the value pointed to by str, so the value of s will not change.

String Constant Pool

There is a String Constant Pool (SCP) in Java

  • Starting with Java 7, it is part of the heap space (previously placed in the method area)

When a string literal is encountered, it will check the SCP

  • If there is A string object A with the same literal content in SCP, A is returned
  • Otherwise, create a new string object D, add it to SCP, and return D
String s1 = "mj"; // String object 'mj' does not exist in SCP
String s2 = "mj"; // There is a string object "mj" in SCP, which directly returns "mj" in SCP
// Therefore, the above two string objects are the same object
System.out.println(s1 == s2); // true

Initialization of string

In C language, character array is String; In Java, the bottom layer of String is composed of char [], but they are not exactly one thing.

Use the breakpoint debugging function of Java to verify the above figure: the representation in the right half is not the real memory address, but it can be understood that if the identification is different, the memory address is different, and if the identification is the same, the memory address is the same. It can be seen that the identifications of s1, s2, s3, s4, cs, s5 and s6 are different; However, the identification of value pointed to by s1, s2, s3 and s4 is 30, which is consistent with that shown in the above figure; The identifier of the value pointed to by s5 and s6 is 31.

intern method

A. Function of intern method:

  • If there is A string object C with the same content as A in SCP, C is returned
  • Otherwise, add A to SCP and return A
int a = 1, b = 2, c = 3;
String s1 = String.format("%d%d%d", a, b, c);
String s2 = String.format("%d%d%d", a, b, c);
// Go to the constant pool and look for "123". If you find no, put the value "123" of s1 into the constant pool
String s3 = s1.intern(); // s3 and s1 both point to "123" in the constant pool
String s4 = s2.intern(); // Return "123" of constant pool
String s5 = "123"; // s5 points to "123" in the constant pool

System.out.println(s1 == s2); // false
System.out.println(s1 == s3); // true
System.out.println(s1 == s4); // true
System.out.println(s1 == s5); // true

Common methods of string (interception)

// Remove the left and right spaces: "123 456"
" 123  456  ".trim();

// To uppercase: "ABC"
// To lowercase: "abc"

// Whether to include a string: true
// Whether to start with a string: true
// Whether to end with a string: true

// Separating strings into arrays: [1, 2, 3, 4]

// Compare the size of the string:<
// Ignore case comparison string size:<

String s1 = "abc";
String s2 = new String("abc");
// Check whether the contents of the string are equal: true
// Ignore case and check whether the contents of the string are equal: true

// String replacement
String str = "hello123";
// Since String is an immutable String, it must be received with a variable
str = str.replace("123", "666"); // hello666

Insert a knowledge point to extract the shortcut key of code block in Eclipse: select the code block first, ALT + SHIFT + M

String interception memory analysis diagram:

String str = "abcdea";
// Interception interval of string: [2, 4)
str.substring(2, 4);  // cd
str.substring(2); 	  // cdea

str.indexOf("a"); 	  // 0
str.lastIndexOf("a"); // 5
str.indexOf("z"); 	// -1

Topics: Java Back-end