Two ways to assign a string

Source: Internet
Author: User

The two assignments of string are different, string str1= "Hello", point to "Hello" in heap memory, and string str2=new string ("Hello"), because new heap memory is opened up, so the address is different, when using = =, The display is false.
Example one:
           String str1= "Hello";
           String str2= "Hello";
           String str3= "Hello";

At this point all three points to the same heap of memory addresses, because if the object pool already has the same string declaration, no more space will be re-opened.
However, if you first use string Str1=new string ("Hello"), open a new heap of memory, the content is "Hello",
Then write string str2= "Hello", the first sentence of new has been opened up, the object pool there is "Hello",
At this point the second sentence should point to the existing address, that is, and the first sentence new opened the same heap memory address Ah,
But why do you use = = when the display is false?
Ask for answers
Because each string is an anonymous object of the string class , it first opens a space in the heap memory to hold the string "Hello" and then uses the heap memory that the keyword new opens, While the heap memory space of the previously defined string constants will not have any stack memory pointing to space, it becomes garbage, waiting to be reclaimed by GC. So, a string object created using a construction method actually opens up two blocks of space, and one of them will be garbage. In addition, the contents of a String class object instantiated with a construction method are not saved in a String object pool and are not capable of sharing data operations.
To observe the problem of pool entry:
public class Stringdemo () {
    public static void Main (string[] args) {
        String str1 = new String ("Hello");
        String str2 = "Hello";
        String STR3 = "Hello";
        System.out.println (str1 = = str2);
        System.out.println (str1 = = STR3);
        System.out.println (str2 = = STR3);
    }
}
Program Run Result:
False
False
True
As you can see from the program above, a string object instantiated using the constructor method is not pooled, so the string class object instantiated by the constructor method can only be used by itself. In the String class, however, a method called manual pooling is provided for ease of operation: public string intern ();
Example: Manually entering a pool
public class stringdemo{
    public static void Main (string[] args) {
      String str1 = new String ("Hello"). Intern (); Into the pool
      String str2 = "Hello";                        Working with pool objects
      String STR3 = "Hello";                        Working with pool objects
      System.out.println (str1 = = str2);             True
      System.out.println (str1 = = STR3);             True
      System.out.println (str2 = = STR3);             True
    }
}
Program Run Result:
True
True
True
This program invokes the Intern () method of the string class after instantiating the object using the String class construction method, which means that the opened string object is saved in the object pool, so the string class object that is completed by the direct assignment is instantiated later, You can simply pull the data out of the object pool and no longer need to re-open new objects.
Common face question analysis: Please explain the difference between the two object instantiation methods of the string class.
   Direct assignment: Only to open up a heap of memory space, the contents of the string can be automatically pooled for the next use;
   Construction method: Open two heap of memory space, one piece will become garbage, and can not automatically into the pool, need to use intern () manually into the pool.
Common face question analysis: Code "string s = new String (" Mldn ");" How many instantiation objects are created for a string class?
   Two instantiated objects were created, one is the anonymous object "Mldn" of the string class, and the other is a string class object instantiated with the keyword new.

Two ways to assign a string

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.