Java variable Storage

Source: Internet
Author: User

1.java Variable Storage domain

The storage area for Java variables is mainly in the following places:

(1) Register: Can be said to be the fastest storage area, in C + + can declare register variables, but in Java cannot declare register variables, but the compiler is determined at compile time.

(2) Stack: Holds references to basic types of data and objects, but the object itself is not in the stack, (through new) objects are placed in the heap or in a constant pool (the constant object of the string is placed in the constant pool).

(3) Heap: Used to store new objects.

(4) Constant pool: constant that holds string constants and basic type data (through public static final decoration).

(5) Static domain: Used to hold static members (statics).

(6) Non-RAM storage: such as hard disk.

The main point here is heap, stack and constant pool. The size and life cycle of the data stored in the stack is determined, and when the referenced data disappears, the stack data is reclaimed by the JVM. The data stored in the heap is uncertain, and when certain conditions are met, it is recycled by the JVM's garbage collection mechanism.

2. Example

such as the following code:

1 string S1 = "Example"; 2 string s2 = "Example"; 3 string s3 = new String ("example"); 4 string s4 = new String ("example");

For S1, S2 will be stored in the constant pool, for S3 (new out of the object), will first go to the constant pool to find there is no "example" object, if any, will create a "example" copy object in the heap, if not, you will first create a "example" in the constant pool Object, and then create a copy object in the heap.

So the question: string str = new String ("abc") creates several objects?

Answer Yue: one or two.

Java variable Storage

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.