Understanding about creating several objects in new string ("xyz ")

Source: Internet
Author: User

Let's first look at a simple program.

Public class t {private string STR = "########### java2000 ################### ##"; public void test () {string S = "@ java2000 @@@@@@@@@@@@@"; string Ss = "########### java2000 #####################"; // same as STR specially }}

Let's take a look at what this class actually generates. Let's open T. Class

The two strings we used already exist. Note that there are two strings, and the repeated strings do not appear twice.

Conclusion:

  1. This type of string is generated during compilation. It can be understood that when the class is loaded, it already exists in the memory and no new
  2. Therefore, "XYZ" in new string ("XYZ") exists during loading. The system allocates memory space based on this template and creates a new object.
  3. As for string STR = new string ("XYZ"); the STR can only be regarded as a reference and a variable. Therefore, only one object is created for this action. Because the "XYZ" already exists before this statement is executed. This statement cannot be calculated.

In short, this interview question mainly aims at understanding the question, and some concepts are difficult to clarify. Although we all know, they have different opinions.

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.