Java Series 2---Do you really know the string object of Java?

Source: Internet
Author: User

? In the previous article said that this article will say that the dynamic binding mechanism of Java, because this knowledge point in the inheritance will be more appropriate, said in this article first to the details of the String object it.

? as long as we learn Java , we all know that there are 8 basic types of Java, but the most common type of String in Java does not belong to this 8 In the base type. He is a class in the Java.lang package. However , the String object's handling of the JVM in reference passing differs from other objects.

? in the formal beginning of this String object, we begin by simply explaining value passing and reference passing in Java. As many Java developers know,Java Programmers do not need to use pointers explicitly, so all objects in Java are passed by reference, although the basic type is passed by value.

? however , objects of type String are passed slightly differently from normal object references. Now let's formally explain the String object.

The string class object is referred to as an immutable string in the J Ava document.

? to understand the above sentence, first understand the two concepts: string variables and String objects.

? A string variable that is used to hold a reference to a string variable whose value is mutable and can be referenced by a different string object as needed.

? string object, in fact, we can understand as a string constant, he is immutable, a string object has been stored in memory since the creation of a fixed position, the character string object is also not modifiable.

And the string object is shared.

? The compiler can let string constants be shared, and we can understand that there is a common string storage pool in the JVM, and each time we create a new string we will save it in this storage pool, but before we save it, He will compare the value of this string object with the existing string object in the pool, if not the same, add in, if any, directly refer to the existing.

? The following section of code can help us understand the concepts above.

String str1 = "Hello";

String str2 = "Hello";

String STR3 = "hell";

The debug results are as follows:

? The above code, we have declared three string variables str1,str2,str3, str1 and str2 copy the same string respectively " Hello", when debug their memory ID is the same, this means that variables str1 and str2 refer to the contents of the same memory block. STR3 are assigned different values, so the memory ID is different.

?

? However, it is important to note that the sharing of string objects is only limited to replication or initialization, and if the concatenation of strings is not present, theJVM will recreate a storage space for each spliced string object, so when a lot of string concatenation is done, We are forbidden to use String objects.

The following code illustrates the problem:

String str1 = "Hello";

String STR3 = str1;

String str2 = "Hello";

String world = "World";

String hw = "Hello world";

String STR4 = Str2+world;

String STR5 = str1 + "" + "world";

The debug results are as follows:

? The above code,STR5 is on the basis of str1 splicing "World", but in debug we can see str The value does not change, and STR5 is not the memory of the referenced str1 , but the newly allocated memory.

Next, I met in SouFun interview with a question about the string object, to apply the above mentioned knowledge points:

Predict the results of the following Java code execution:

? public static void Main (string[] args) {

String str = new String ("John");

StringBuffer SBF = new StringBuffer ("John1");

Stringtest (str);

Stringbuffertest (SBF);

System.out.println (str+ "---" +SBF);

}

public static void Stringtest (String str1) {

STR1 = str1+ "Test";

}

public static void Stringbuffertest (StringBuffer sbf1) {

Sbf1.append ("test");  

}

What are the results of the predictions? Are the results consistent with the implementation?

Here is the result of the execution:

Let's take a detailed explanation of the reasons for the results:

The above variables, str and SBF, are references to objects, which are referred to when calling Stringtest () and Stringbuffertest (), but why did the string object not spell the "test" string? I use the following diagram to illustrate the problem.

So everyone should have an understanding of the string object. If everyone finds out what I said is wrong, I hope you can point it out.

The following is my public number, technical Daniel Assembly number, welcome your attention!

Java Series 2---Do you really know the string object of Java?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.