Integer127 and 128

Source: Internet
Author: User
Tags assert
127 and 128 of integers
Integer a = 127;
Integer B = 127;
System.out.println (a==b);
Extra byte will redistribute space
Integer c = 128;
Integer d = 128;
System.out.println (C==d);

Run Result: True,false

First, it must be clear that if you compare two objects, "= =" is the address, and "equals" is the content


As you can see, the addresses of A and B are the same, and C and D are different. Analysis:

The JVM automatically maintains a constant pool of eight basic data types, and the scope of the -128~127 is initialized in the INT constant pool

When an integer a = 127 is executed, a numeric object in the constant pool is taken during the automatic boxing process, and in fact it executes such a method (JDK source code):

public static Integer valueof (int i) {  
       assert integercache.high >= 127;  
       if (i >= integercache.low && i <= integercache.high) return  
           Integercache.cache[i + (-integercache.low )];  
       return new Integer (i);  
   

There is a Integercache class:

private static class Integercache {static final int low =-128;
        static final int high;

        Static final Integer cache[];
            static {//high value May is configured by property int h = 127;
            String Integercachehighpropvalue = Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high"); if (integercachehighpropvalue!= null) {try {int i = parseint (Integercach
                    Ehighpropvalue);
                    i = Math.max (i, 127);
                Maximum array size is integer.max_value h = math.min (i, Integer.max_value-(-low)-1); catch (NumberFormatException NFE) {//If The property cannot is parsed into int, ignore it
                .

            } high = h;
            cache = new Integer[(high-low) + 1];
            int j = Low; for (int k = 0; k < cache.length K+ +) Cache[k] = new Integer (j + +);
        range [ -128, 127] must be interned (JLS7 5.1.7) assert Integercache.high >= 127; Private Integercache () {}}

In this class, it maintains an integer cache[] array, which holds the -128~127 wrapper class object .
  
When executing the Integer c = 128, 128 is not in the range of values maintained by the constant pool, so a new Integer (128) is required in the automatic boxing process to allocate space on the heap memory.
So the C and D addresses are not the same.

If you change the start code to:

Integer a = new integer (127);
Integer b = new integer (128);
System.out.println (A = = B);

Then, the address of A and B is different.

Similarly, the other basic types of constant pool related issues are similar.

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.