Constant Pool Summary

Source: Internet
Author: User

the constant pool in Java is actually divided into two forms: a static constant pool and a run-time pool. The so-called static constant pool, the constant pool in the *.class file, is a constant pool in a class file that contains not only string literals, but also classes, methods, and most of the space in the class file. running a constant pool is a JVM virtual machine that, after completing the class mount operation, loads the constant pool in class file into memory and saves it in the method area, which is what we often call a const pool, which refers to the run-time pool in the method area. Use the following example to explain the constant pool:
1String S1 = "Hello";2String s2 = "Hello";3String s3 = "Hel" + "Lo";4String S4 = "Hel" +NewString ("Lo");5String S5 =NewString ("Hello");6String s6 =S5.intern ();7String s7 = "H";8String s8 = "Ello";9String S9 = s7 +S8;Ten           OneSystem.out.println (S1 = = s2);//true ASystem.out.println (S1 = = S3);//true -System.out.println (S1 = = S4);//false -System.out.println (S1 = = S9);//false theSystem.out.println (S4 = = S5);//false -System.out.println (S1 = = S6);//true
first of all, in Java, the direct use of the = = operator, compared to two strings of the reference address, not the comparison content, compare the content please use String.Equals (). S1 = = S2 This very good understanding, S1, S2 in the assignment, all use the string literal, white point, is to write the string directly to die, during the compilation, this literal will directly into the class file in the constant pool, so as to achieve reuse, loaded into the run-time pool, S1, S2 points to the same memory address, so it is equal. S1 = = S3 This place has a hole, S3 although the dynamic splicing out of the string, but all the parts involved in the stitching is known literal, during the compilation, this splicing will be optimized, the compiler directly help you spell, so string s3 = "Hel" + "lo"; The class file is optimized to string s3 = "Hello", so S1 = = S3 is established. S1 = = S4 Of course not equal, S4 although also splicing out, but the new string ("Lo") this part is not known literal, is an unpredictable part, the compiler will not be optimized, you must wait until run time to determine the results, combined with the string invariant theorem, The ghost knows where the S4 is assigned, so the address must be different. With a schematic diagram to clarify the idea:S1 = = S9 is not equal, the truth is similar, although S7, S8 in the assignment when the string literal used, but stitching into S9, S7, S8 as two variables, are unpredictable, compiler is compiler, it is not possible when the interpreter, so do not optimize, wait until the run, S7, S8 The new string, the address in the heap is not deterministic and cannot be the same as the S1 address in the method area constant pool. S4 = = S5 have no explanation, absolutely not equal, both are in the heap, but the address is different. S1 = = S6 The two equals are entirely attributed to the Intern method, S5 in the heap, the content is Hello, the Intern method attempts to add the Hello string to the constant pool and returns its address in the constant pool, because the constant pool already has a Hello string, So the Intern method returns the address directly, whereas S1 points to the constant pool at compile time, so S1 and S6 point to the same address, equal. at this point, we can draw three very important conclusions: 1, must pay attention to the compilation period behavior, can better understand the constant pool. 2. Constant in the run-time pool, basically from the constant pool in each class file. 3, when the program runs, unless you manually add constants to the constant pool (such as calling the Intern method), the JVM does not automatically add constants to the constant pool. the above only refers to the string constant pool, in fact there are integer constant pool, floating-point constant pool and so on, but all the same, but the constant pool of numeric types can not manually add constants, the program starts constant pool constants are determined, such as the constant range in the integer constant pool: -128~ 127, only the number of this range can be used in a constant pool. Reference: deep understanding of Java Virtual Machine ——— JVM advanced features and best practices

Constant Pool Summary

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.