I want to discuss with my colleagues about the memory storage of string as follows:
String S = "ABC ";
String T = new string ("ABC ");
S t is stored in the stack as an object reference. My question is, what is the object information behind it? If new is used to initialize T, "ABC" must be stored in the heap. Will the "ABC" corresponding to S be stored in the stack or heap...
Please give me some advice
Bytes -----------------------------------------------------------------------------------
Solution to the string problem:
First of all, many people first think that this type should be complex, but is it true? The following is an analysis from several aspects.
First, create a string
String S = "ABC ";
This expression is the declaration method of a classic string-type instance, where S is a reference to "ABC" and "ABC" is its object content, let's call it an object here (in Java, the basic type does not belong to an object, but there are corresponding packaging classes to implement their object functions ), s and "ABC" are both stored in the stack.
It should be emphasized that there are two storage areas in Java's memory allocation mechanism: Stack and heap. The stack stores program processes, object references, basic types, and this odd object string.
In the stack, Java sets up a string container-buffer pool to allow programmers to operate strings easily. In the preceding example, when we declare the S variable, the following steps are taken: first, the JVM goes to the buffer pool to find "ABC". The buffer pool is initially initialized at each thread. If yes, the existing "ABC" is handed over to S. If no, the "ABC" is stored in the buffer pool and its address is paid to S, in this way, the entire payment operation is completed.
This is the simplest example. Here, we may have a problem:
String S = "ABC ";
String T = "ABC ";
System. Out. println (S = t );
What is the output result? The result is true. Based on the above discussion, we can easily get this answer. Please refer to the following example.
String S = new string ("ABC ");
String T = new string ("ABC ");
System. Out. println (S = t );
The output is false. It can be seen that this is the nausea of the string, which is like the package and basic type... Well, we will not discuss its ownership for the moment. After all, sun's Daniel is designed for the sake of programmers. In the above example, we can see that S and T are all created in the new method. Here, S and T are stored in the stack, and they are references to objects, the two objects are stored in the heap. As new Orthodox objects, they can only stay in the heap.
Although they are all strings, their content is the same, but they are also very different because of different initialization methods.
After a simple question is completed, the following describes a more complex section.
String S1 = "ABC ";
String S2 = "def ";
String S3 = "abcdef ";
String S4 = S1 + S2;
System. Out. println (S3 = S4 );
System. Out. println (s3.equals (S4 ));
In the above example, we can see that the first output is false, and the second output is true. my problem is, if the string adopts the pool policy in the strong stack, why is the first output false? Obviously, S3 points to the object "abcdef ", the object of S4 is "abcdef". Both methods are declarative, rather than new initialization. The answer is to look at the mechanism designed by Sun's experts. In this string declaration method, S4 = S1 + S2 returns different results from S4 = "abcdef". In this case, both S4 and S3 exist in the stack pool, however, S3 corresponds to "abcdef", while S4 is not a simple "abcdef". Of course, the value of S4 is "abcdef. Let's do a test:
String S1 = "ABC ";
String S2 = "def ";
String S3 = "abcdef ";
String S4 = S1 + S2;
String S5 = "abcdef ";
System. Out. println (S5 = S3 );
System. Out. println (S5 = S4 );
The answer is that the first output is true, and the second output is false.
The mechanism for declaring through the "+" connection is that S4 and its corresponding computing result "abcdef" are stored in the stack. However, when declaring S4, instead of calculating the result "abcdef" first, it allocates a space directly in the pool and stores something. The specific storage is a reference pointing to S1 and S2, it is still stored as "ABC" "def". I don't know, and I didn't come up with a good way to test it. It's always not "abcdef ".
Here we can see that the declaration of string S = "ABC"; and string S = new string ("ABC"); are clear, but the string S = S1 + S2; is not that clear, and there are many problems, such as whether there is a relationship between S and S1 and S2? I conducted the following experiments:
String S1 = "ABC ";
String S2 = "def ";
String S3 = S1 + S2;
System. Out. println (S3)
S2 = S2 + "Java ";
System. Out. println (S3 );
The above result is the first output abcdef, and the second output is abcdef. After verification, I found that S2 = S2 + "Java"; in the equal sign is not a S2, but two completely different addresses. After the JVM receives S2 + "Java", it immediately allocates a new space and updates the original content of S2. Therefore, is the result of S3 related to the original S2 and S1, I don't know, but I'm sure that string is a static variable. Even if it provides the Concat () method, it returns another string.
Here we come to the conclusion that we should regard the variable declared by string as a static variable or constant. If we need to calculate it, we 'd better use stringbuffer, in a sense, it makes up for the string defect.
The contents of the string declaration end here. The following describes the use of string.
First, transmission problems
Class checkstring {
Public String checkone (string t ){
Return T;
}
Public String checktwo (string s ){
Return S + "Java ";
}
Public static void main (string ARGs []) {
Checkstring cs = new checkstring ();
String T = "ABC ";
String S = cs. checkone (t );
System. Out. println (t = s );
String M = cs. checktwo (t );
System. Out. println (t );
System. Out. println (m );
}
}
The output result is: the first is true, the second is ABC, and the third is abcjava;
The purpose of this experiment is to verify that in Java, the basic type transfer adopts the value transfer method, while the complex type transfer adopts the reference transfer method. In this example, we can see that the first true indicates that T and S point to the same memory space. According to the previous discussion about the initialization part of string, we can export this result. The second output is ABC, indicating that the T value does not change after T is passed and T is processed. This indicates that the input is not a reference of "ABC, it is a copy of "ABC.
So far, we should no longer regard string as an object, although it is indeed an object... It does not match too many objects... In future applications, if you need to calculate strings, you must use stringbuffer. This is not only a habit, but also a matter of code robustness.
From: http://hi.baidu.com/whofly/blog/item/93792adf123a251548540300.html
I have benefited a lot from reading this introduction to string! Although it is one of the frequently used types, it is always ignored!