About string s = new string ("XYZ"); Problems with creating several objects

Source: Internet
Author: User

Quote from this friend: http://blog.sina.com.cn/s/blog_6a6b14100100zn6r.html

You know, in Java, in addition to the basic types in 8, the others are class objects and their references. So "xyz" is a string object in Java. For a string class object, his object value cannot be modified, that is, invariant.


See:
String s= "Hello";
S= "Java";
String s1= "Hello";
String S2=new string ("Hello");

Ah, is the string object referenced by s not modified? What did you say about the invariance that went there?

Don't worry about it, let me tell you what happened:
During the JVM's work, a piece of memory space is created dedicated to the string object. We call this memory space a string pool.

String s= "Hello"; when the JVM sees "Hello", a String object is created in the string pool to store it and return his reference to S.
S= "Java", when the JVM sees "Java", creates a new string object in the string pool to store it, and returns a reference to the new string object to S. The original "Hello" is still inside the string pool. Did not disappear, he could not be modified.

So we just changed the reference to S, without changing the object He was referencing, because the value of the string object cannot be modified.

String s1= "Hello"; the JVM first looks inside the string pool for not finding the string "Hello", finding, returning his reference to S1, otherwise creating a new string object and putting it into the string pool. Because s= "Hello" is here, the object is already referenced, so both the rule S and the S1 refer to the same object. So S==S1 will return true. (= =, for non-basic types, is to compare whether two references refer to the same object in memory)

String s2=string ("Hello"); the JVM first looks inside the string pool for not finding the string "Hello", finding it, not doing anything else, creating a new string object and putting it inside the string pool. Because new is encountered, a string object is created in memory (not inside the string pool) to store "Hello" and a String object on memory (not in the string pool) is returned to S2. So S==s2 will return false, not referencing the same object.

OK now we look at the topic:
string s = new string ("XYZ");
First, find it inside the string pool. Does not create a string object, otherwise it is created so that a string object
The new operation symbol is encountered, a string object is created in memory and returned to S, and another object

so it's a total of 2 objects .

An example:
public class Test
{
public static void Main (String []args)
{
String s1=newstring ("test");//create 2 objects, one class and one heap inside
strings2= "test";//create 1 objects, S2 point to the "test" object inside the pool
strings3= "test";//Create 0 objects, point to S2, refer to the object in the pool.
strings4=s2;//creates 0 objects, pointing to s2,s3 the object in the pool.
String s5=newstring ("test");//create 1 objects inside the heap, note that it's okay with S1

System.out.println (s2== "test");//trues2== "Test" is obviously true
System.out.println (S2==S3),//true, because the point is the "test" in the pool.
System.out.println (S2==S4),//true, ibid., then S3 and S4 ...:)
System.out.println (S1==S5);//false, obviously, false.
System.out.println (S1==S2);//false, the object is different.
System.out.println (s1== "test");//false, does s1!= "Tset"? Let's talk.

System.out.println ("---------------");

S1=S2;
System.out.println (s1== "test");//true, here.
}
}
Description: 1,system.out.println (S1==S2); Obviously, the object "test" pointed to by S2 is inside the pool, and S1 is pointing to the "test" object in the heap (the memory area where the S1 points), so it returns false.
2,system.out.println (s1== "test"), S1 points to the "test" object in the heap (S1 points to the memory area), and "Test" is the program just established (in fact, the shared pool of the already created "test" object, Which is what we created in the pool when we s2= "test", so the "test" object in the heap that S1 points to
and "Test" (inside the pool) is not an object, so return false.
3, when we s1=s2, it is clear that the S2 point to the S1,S1 to the pool inside the "test", this time, S2 also pointed to the pool inside the "Test" object, when System.out.println (s1== "test"); The Java Virtual machine creates a "test" object, noting that, in fact, it was not created, as previously said, public s1= "test" created by "Test" object (inside the pool), so, s1== "test" (inside the pool), similarly, s1=s2=s3=s4!

And why do you say string s=newstring ("test") on the Internet; 2 objects created? That might be because it wrote a code, mistakenly let a person default that the execution of the code is not an instance of any string object before (perhaps many people do not think so,), followed by others or do not think about the 2, pour is that storage in the stack memory dedicated string object reference to the S variable is an Object! I can't forgive you!

About string s = new string ("XYZ"); Problems with creating several objects

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.