A detailed explanation of string memory location based on Java _java

Source: Internet
Author: User
Tags constant

Objective

Previously wrote an article on the JVM Memory Area division, but yesterday received an ant job interview, asked the JVM related content, explain the JVM's memory area partition, this part of the answer is not bad, but then asked the Java string stored in the location, Just remember that string is a constant amount, should be stored in a constant pool, but later asked where the new string should be placed, this should be placed in the heap, and then asked where the string reference is placed, then the idiot said it was put in the heap inside , now summarize: The basic type of variable data and object references are placed inside the stack, the object itself in the heap, the explicit string constants in the constant pool, the string object in the heap.

Description of the constant pool

The constant pool is placed in the method area, which is in the permanent generation, from the JDK7 to the heap. This change can be seen from the * * Important Rfes addressed in JDK 7 from Oracle's release version notes.

Area:hotspot
synopsis:in JDK 7, interned strings are no longer allocated in the permanent generation of the Java Hea P, but are instead allocated in the main part of the Java heap (known as the "Young and Old Generations"), along with the OT Her objects created by the application. This change would result in the more data residing in the main Java heap, and less data in the permanent generation, and thus M Ay require heap sizes to be adjusted. Most applications'll you'll be relatively small differences in heap usage due to this change, but larger-applications tha T load many classes or make heavy with the String.intern () method would be more significant differences.
rfe:6962931

String Memory Location Description

1. An explicit string constant

String a = "Holten";
String B = "Holten";

• A String object with a value of Holten is created in the constant pool after the first code executes;

• When the second sentence executes, a new string object is no longer created because there is a holten in the constant pool.

• The reference to the string is now inside the virtual machine stack.

1.String objects

String a = new string ("Holtenobj");
String b = new String ("Holtenobj");

When class is loaded, a string object with a value of holtenobj is created in the constant pool, and the first sentence executes with the new string ("Holtenobj") object created in the heap;

• When the second sentence executes, a new string object is created directly in the heap, because holtenobj exists in the constant pool.

Verify

/**
 * Created by Holten.gao on 2016/8/16.
 *
/public class Main {public
  static void Main (string[] args) {
    String str1 = "Upper primary day";
    String str2 = "Gao Tian";
    System.out.println (STR1==STR2);//true
    
    String str3 = new String ("Tall Day");
    String STR4 = new String ("Tall Day");
    System.out.println (STR3==STR4);//false
  }
}

return Result:

True
false

The above is based on the Java string memory location is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.