String exploration in Java

1.String

1. The String class is immutable after initialization, and
its member methods default to final methods. Any related change operation will generate a new object.

String str1 = "abc";
str1 = "bcd";
String str2 = new String("abc");
String str3="a"+"bc";
System.out.println(str1==str3); //true

At this time, str1 just points to the "bcd" string constant in the string constant pool.
The object generated by the new keyword is stored in the heap, and the reference is stored in the stack, that is, str1 and str2 are stored in the stack, the "abc" generated by new is stored in the heap, and it points to "bcd" in the constant pool.

There is a string constant pool in the jvm method area. Whenever we create a string constant, the JVM will first check the string constant pool. If the string already exists in the constant pool, then directly return the instance reference in the constant pool. If the string does not exist in the constant pool, the string is instantiated and placed in the constant pool. Due to the immutability of String strings, we can be quite sure that there are no two identical strings in the constant pool.
The constant pool in Java is actually divided into two forms: static constant pool and runtime constant pool.

(1) Strings created using "" quotation marks alone are constants, and they are determined to be stored in the String Pool at compile time; (2) Objects created using new String ("") will be stored in the heap, which is new at runtime Created

2.String + sign

String s = "a" + "b" + "c"; 
String s1  =  "a"; 
String s2  =  "b"; 
String s3  =  "c"; 
String s4  =  s1  +  s2  +  s3;

The creation of s only generates one object, while s4 generates two objects, and each time one object is generated;
The literal "+" stitching is done during compilation. The "+" splicing operation referred to by the string is actually performed at runtime, and the newly created string is stored in the heap.

2. Distinguish ==and equals

(1) For ==, if it acts on a variable of basic data type (byte, short, char, int, long, float, double, boolean), then directly compare whether the stored "value" is equal; if it acts on a reference type Variable (String), the address of the pointed-to object is compared (that is, whether it points to the same object).

(2) The equals method is a method in the base class Object, so this method is available for all classes that inherit from Object. In the Object class, the equals method is used to compare whether the references of two objects are equal, that is, whether they point to the same object.

(3) For the equals method, note that the equals method cannot be applied to variables of basic data types. If the equals method is not rewritten, the address of the object pointed to by the variable of the reference type is compared; and the String class rewrites the equals method to compare whether the strings stored in the pointed string object are equal . Some other classes such as Double, Date, Integer, etc. have rewritten the equals method to compare whether the contents of the pointed-to objects are equal.

3. The difference between String, StringBuffer, StringBuilder

(1) The objects in String are immutable, which can be understood as constants, obviously thread-safe; most of the methods in StringBuffer are modified by the synchronized keyword, so they are thread-safe; StringBuilder is not thread-safe;

4.String and StringBuffer pass value

public static void main(String[] args) {
		//1.String类型
		String str1="hello";
		String str2="world";
		System.out.println(str1+"----------------"+str2);//hello----------------world
		change(str1,str2);
		System.out.println(str1+"----------------"+str2);//hello----------------world
		
		//2.StringBuffer类型
		StringBuffer sb1=new StringBuffer("hello");
		StringBuffer sb2=new StringBuffer("world");
		System.out.println(sb1+"-----------------"+sb2);//hello----------------world
		change2(sb1,sb2);
		System.out.println(sb1+"-----------------"+sb2);//hello----------------worldworld
	}
 
	private static void change2(StringBuffer sb1, StringBuffer sb2) {
		sb1=sb2;
		sb2=sb1.append(sb2);
	}
 
	public static void change(String str1, String str2) {
		str1=str2;
		str2=str1+str2;
	}

Only values ​​are passed in Java, but it is important to know whether the value is the actual value of the variable or the address of the referenced object.
In the change () method, str1 and str2 change the pointed object, but it has nothing to do with the main method;
the object pointed to by sb2 in the change2 () method itself changes, and sb2 in main also points to that object, which naturally affects the variables in main.

Published 137 original articles · Like 123 · Visit 250,000+

Guess you like

Origin blog.csdn.net/lz20120808/article/details/85952526
Recommended