String constant pool size conversion reflection operation StringBuffer and StringBuilder

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


1. String constant pool

The constant pool is a memory area divided by the JVM. When the JVM starts running, it will make a special area as the constant pool.
If the direct assignment mode is used to instantiate the String class object, then the instantiated object (string Content) will be automatically saved in this object pool.

If you continue to use the direct assignment mode to declare String objects next time, if there is specified content in the object pool at this time, it will be directly referenced

If not, then create a new string object and save it in the object pool for next use

2. String reflection operation

Reflection is an important feature of object-oriented programming. Some programming languages ​​are also called “introspection”. It
refers to obtaining/modifying detailed information (type information, attribute information, etc.) of an object during the running of the program, which is equivalent to letting An object better "recognizes itself".
Using reflection in Java is more troublesome. We will introduce the specific usage of reflection in detail in a later course.
Why is String immutable? (What are the benefits of immutable objects?

  1. It is convenient to implement string object pool. If String is variable, then the object pool needs to consider when to deep copy the string.
  2. Immutable objects are thread-safe.
  3. Immutable objects are more convenient to cache the hash code, and when used as keys, they can be stored in HashMap more efficiently.
    Note: The following code should not be produced in your development, it will generate a large number of temporary objects, and the efficiency is relatively low.**
String str = "Hello";
// 获取 String 类中的 value 字段. 这个 value 和 String 源码中的 value 是匹配的.
Field valueField = String.class.getDeclaredField("value");
// 将这个字段的访问属性设为 true
valueField.setAccessible(true);
// 把 str 中的 value 属性获取到.
char[] value = (char[]) valueField.get(str);
// 修改 value 的值
value[0] = 'h';
System.out.println(str);

Execution result hello

3. Some operations in the string

1. Remove the left and right blank characters in the string (spaces, newlines, tabs, etc.)

s1.trim();

2. String to uppercase (to lowercase)

  s1.toUpperCase();
  s1.toLowerCase();

3. String into the pool operation

s2.intern();
String str1 = new String("hello"),intern();

Search first, enter the pool if it is not found, and return to the location. After entering the pool, the new space will be recycled as garbage space.

4. String connection (equivalent to +)

4.StringBuffer 和StringBuilder

Generally speaking, the operation of String is relatively simple, but due to the immutable nature of String, in order to facilitate string modification, StringBuffer and StringBuilder classes are provided.
Most of the functions of StringBuffer and StringBuilder are the same. It mainly introduces StringBuffer
using "+" in String to connect strings, but this operation needs to be changed to append() method in StringBuffer class.

public class Test{
    
    
	public static void main(String[] args) {
    
    
		StringBuffer sb = new StringBuffer();
		sb.append("Hello").append("World");
		fun(sb);
		System.out.println(sb);
}
public static void fun(StringBuffer temp) {
    
    
	temp.append("\n").append("www.bit.com.cn");
}
}

1. String reverse

StringBuffer sb = new StringBuffer("helloworld");
System.out.println(sb.reverse());

2. Delete the specified range

StringBuffer sb = new StringBuffer("helloworld");
System.out.println(sb.delete(5, 10));

3. Insert data before specifying the position

StringBuffer sb = new StringBuffer("helloworld");
System.out.println(sb.insert(0, "lala"));

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/112909755