On the storage location of string constants in Java

Source: Internet
Author: User

Before we tell you about this, we need some preliminary knowledge:

The memory in Java is divided into the following sections:

1, the stack area: The compiler automatically allocates the release, after the concrete method executes, the system automatically frees the JVM memory resources.

Its role is to save the values of local variables, including: 1. The value used to hold the base data type; 2. Save an instance of the class, which is a reference (pointer) to the heap object . It can also be used to save the frame when the method is loaded.

2, heap area: generally by the programmer to allocate the release, the JVM does not periodically view this object, if there is no reference to this object is recycled.

It acts to store dynamically generated data, such as new objects . Note the objects that are created contain only the member variables that belong to them, and do not include member methods.

Because objects of the same class have their own member variables stored in their own heap, they share the methods of the class, and not every object is created to copy the member method one at a time.

3. Code Area : The binary code that stores the method in the program, and multiple objects share a region of code space .

4. Data area: static member used to store static definition.

5, Chang: The JVM maintains a constant pool for each loaded type, and the constant pool is an ordered set of constants used by this type. Includes direct constants (basic types, String) and symbolic references to other types, methods, and fields. The data in the pool is accessed through the same index as the array. Because a constant pool contains symbolic references to other types, methods, and fields for one type, Chang plays a central role in the dynamic linking of Java. A constant pool exists in the heap .

Roughly describes the memory allocation in Java

Then let's take a look at a code example:

1  Public classTeststringconstant {2      Public Static voidMain (String args[]) {3         //string constants, which are assigned to the data Segment, are optimized by the compiler.4         //that is, when a string already exists, the same object is not created again, and the S2 is pointed directly to "Hello".5String S1 = "Hello"; 6String s2 = "Hello";7         //new objects are allocated in the heap. S3 and S4 Although they point to the same string content, they are two different objects.8         //so = = The reference is different when it is compared, so it is not equal9String s3 =NewString ("World"); TenString S4 =NewString ("World"); One          ASystem.out.println (S1 = = s2);//true -System.out.println (s3 = = S4);//false -System.out.println (S3.equals (S4));//true the         //The equals method in string has been rewritten, comparing whether the content is equal. -     } -}

I believe you have a more vivid understanding of the allocation area of string constants and the memory allocation of Java.

Here are some of the relevant knowledge points to add and note:

1. distinguish what an instance is and what an object is. Class a= New Class (), at which point A is called an instance, not a is an object. The instance is in the stack, the object is in the heap, and the operation instance is actually manipulating the object indirectly through the pointer of the instance. Multiple instances can point to the same object.

2. data in the stack is not synchronized with data destruction in the heap. Once the method ends, the local variables in the stack are destroyed immediately, but the objects in the heap are not necessarily destroyed. Because there may be other variables that point to this object, it is destroyed only if there are no variables in the stack that point to the object in the heap, and it is not destroyed immediately, and can be destroyed when the garbage collection is scanned.

3. The above stacks, heaps, code snippets, data segments, and so on are all relative to the application. Each application corresponds to a unique instance of the JVM, and each JVM instance has its own area of memory that does not affect each other. And these memory areas are shared by all threads. The stacks and heaps mentioned here are concepts on the whole, and these stacks can be subdivided.

4. the member variables of a class vary in different objects and have their own storage space (the member variable is in the object in the heap). The method of the class is shared by all objects of the class, only one set, the method is pressed into the stack when the object is used, and the method does not consume memory.

Note: The above only covers the two parts of stack and heap, but there is also a more troublesome part-constant pool. This article is no longer described here, you want to understand the following things:

Http://www.blogjava.net/Jack2007/archive/2008/05/21/202018.html

Http://developer.51cto.com/art/201009/225071_1.htm

On the storage location of string constants 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.