-
s = new String("xyz"); How many String Objects are created? Two objects, one is "xyx" and the other is the reference object s pointing to "xyx".
String s="Hello";int i=3; s=i+s; Is this expression correct? In java, it will prompt that the data type does not match. Because string is a class! Correct approach: s+="3" or s+='3' or s+=(char)i;
We're going to introduce another way to create String objects - text enclosed in quotes. This method is unique to String, and it is very different from the new method.
There is a string pool in the JAVA Virtual Machine (JVM), which stores many String objects and can be shared, so it improves efficiency. String a="abc";, when this line of code is executed, the JAVA virtual machine first searches the string pool to see if such an object with the value "abc" already exists. The basis for judgment is String class equals(Object obj) The return value of the method. If there is, no new object will be created, and a reference to the existing object will be returned directly; if not, the object will be created first, then added to the string pool, and then its reference will be returned.
Creation of string objects: Due to the extensive use of string objects [it is an object, generally speaking, objects always allocate memory in the heap], in Java, in order to save memory space and running time [such as when comparing strings, == is better than equals()], all string literals are put into a literal pool during the compilation phase, and the runtime literal pool becomes part of the constant pool. The advantage of the literal pool is that all the same string constants in the pool are merged and only occupy one space. We know that for two reference variables, use == to determine whether their values [references] are equal, that is, they point to the same object:
Now look at the String s = new String("abc"); statement. Here "abc" itself is an object in the pool, and when new String() is executed at runtime, a copy of the object in the pool is placed in the heap. , and hand over the reference of this object in the heap to s. ok, this statement creates 2 String objects.
String s1 = new String("abc") ;String s2 = new String("abc") ;if( s1 == s2 ){ //Statement that will not be executed}
//How many String Objects were created? [Three, one in the pool and two in the heap. ]
Only new objects generated by "+" connection between String objects created using quotation marks to include text will be added to the string pool. For all "+" connection expressions that contain new objects created in the new method (including null), the new objects they generate will not be added to the string pool.
1.== means that they are referenced from the same object, and equals() means that the values are equal.
String str1 = "abc"; the referenced object is on the stack (or String pool).
String str1 =new String ("abc"); The referenced object is in memory/heap.
2.String str1 = "string"; in the stack
String str3 = "str"; on the stack
String str4 = "ing"; on the stack
String str2 = str3+str4; in the heap, because the function of the + sign is to return another newly created String object instead of looking for the string value on the stack. If it is String str2 = "str"+"ing"; then the final result is on the stack. str1==str2 is true.
But there is one situation that demands our attention. Please look at the code below:
public class StringStaticTest {
public static final String A = "ab"; // constant A
public static final String B = "cd"; // constant B
public static void main(String[] args) {
String s = A + B; // Initialize s by connecting the two constants with +
String t = "abcd";
if (s == t) {
System.out.println("s equals t, they are the same object");
} else {
System.out.println("s is not equal to t, they are not the same object");
}
}
}
The result of running this code is as follows:
s is equal to t, they are the same object
The reason is that in the above example, A and B are constants and their values are fixed, so the value of s is also fixed, and it has been determined when the class is compiled. That is to say: String s=A+B; is equivalent to: String s="ab"+"cd";
Let me change the above example slightly and see what happens:
public class StringStaticTest {
public static final String A; // constant A
public static final String B; // constant B
static {
A = "ab";
B = "cd";
}
public static void main(String[] args) {
// Initialize s by connecting the two constants with +
String s = A + B;
String t = "abcd";
if (s == t) {
System.out.println("s equals t, they are the same object");
} else {
System.out.println("s is not equal to t, they are not the same object");
}
}
}
The result of its operation is this:
s is not equal to t, they are not the same object
Just made a little change, and the result is exactly the opposite of the example just now. Let’s analyze it again. Although A and B are defined as constants (can only be assigned once), they are not assigned immediately. Before the value of s is calculated, when they are assigned and what value they are assigned are all variables. Therefore, A and B behave like a variable before being assigned a value. Then s cannot be determined at compile time, but can only be created at runtime.
Finally, let’s talk about the storage of String objects in the JAVA Virtual Machine (JVM), and the relationship between the string pool and the heap and stack. Let’s first review the difference between heap and stack:
Stack: Mainly saves basic types (or built-in types) (char, byte, short, int, long, float, double, boolean) and object references. Data can be shared, and its speed is second only to register. Faster than heap.
Heap: used to store objects.
When we look at the source code of the String class, we will find that it has a value attribute, which stores the value of the String object. The type is char[], which also shows that a string is a sequence of characters. When executing String a="abc";, the JAVA virtual machine creates three char values 'a', 'b' and 'c' in the stack, and then creates a String object in the heap, its value (value ) is an array of three char values just created on the stack {'a', 'b', 'c'}. Finally, the newly created String object will be added to the string pool.
If we then execute the String b=new String("abc"); code, since "abc" has been created and saved in the string pool, the JAVA virtual machine will only create a new String object in the heap, but its The value is the three char type values 'a', 'b' and 'c' created on the stack when the previous line of code was executed.
At this point, we are already quite clear on the question of why String str=new String("abc") raised at the beginning of this article creates two objects.
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/yakihappy/archive/2009/03/10/3977169.aspx
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/Foxalien/archive/2009/12/18/5029470.aspx
-