The embodiment of Java basic data types in constant pools __java

Source: Internet
Author: User

How Java basic data types are represented in a constant pool

Amount

The use of basic data types is very large, Java constant pool, in memory savings is a good mechanism, the same data, in a constant pool as long as a copy can be.

8 Basic types of Java (Byte, short, Integer, Long, Character, Boolean, float, double), except for Float and double, all six of them implement a constant pool. However, they use constant pools only when they are greater than or equal to 128 and are less than or equal to 127.

As can be seen from the following example:

The results are as follows:

Equals 127:
True
*****************
equals 128:
False
*****************
Equals-128:
True
*****************
Equals-129:
False
*****************
Test Boolean
True
False

When we assign a value to an integer, we actually call the integer.valueof (int) method to see the source code, which is implemented as follows:

And the Integercache implementation is as follows:

Note that the cache array is static.

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.