About the memory location of string in Java

Source: Internet
Author: User

The Java runtime memory is divided into five parts:

Thread sharing: Heap memory, method area (including Chang)

Thread private: Stack memory, local method stack, program counter

String is not a basic data type, what is the memory location of a string? A total of two cases:

1, String Direct assignment:

String s = "haha";

The reference to S is present in the stack memory, and the reference point to the haha exists in the constant pool of the method area (first to determine if there is a haha in the constant pool, which is directly pointing to the existence)

2. String Object New Creation

string s = new string ("haha");

The reference to S is present in the stack memory, referencing the Haha object that exists in heap memory (every new time, creating a new Haha object in the heap)

Summarize:

1, a string type of reference, are present in the stack memory;

2, string reference point, the direct assignment exists stack memory, new out of the existing stack memory.

About the memory location of string in 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.