Two pairs of integers are exactly the same, why is one output true and one output false?

Source: Internet
Author: User

Here's a piece of code:

publicclassMain {

     public static void main(String[] args) {                   Integer i1 =  100 ;          Integer i2 =  100 ;          Integer i3 =  200 ;          Integer i4 =  200 ;                   System.out.println(i1==i2);          System.out.println(i3==i4);      } }Its output is this: true false; The reason is simple:

The output shows that I1 and I2 point to the same object, while i3 and I4 point to different objects. At this point only a look at the source code will know, the following is an integer valueof method of the specific implementation:

public static Integer valueOf (int i) {        if (i >= -128 && i <= integercache.high)            return integercache.c Ache[i + +];        else            return new Integer (i);    }
public static Integer valueOf (int i) {        if (i >= -128 && i <= integercache.high)            return Integercache . cache[i + +];        else            return new Integer (i);    }

Where the Integercache class is implemented as:

private static class Integercache {        static final int high;        Static final Integer cache[];        static {            final int low = -128;            High value is configured by property            int h = 127;            if (integercachehighpropvalue! = null) {                //use Long.decode. Avoid invoking methods that                //require Integer ' s autoboxing cache to be initialized                int i = Long.decode (integercachehighpropvalue). Intvalue ();                i = Math.max (i, 127);                Maximum array size is integer.max_value                h = math.min (i, Integer.max_value--low);            }            High = h;            cache = new Integer[(high-low) + 1];            int j = Low;            for (int k = 0; k < cache.length; k++)                cache[k] = new Integer (j + +);        }        Private Integercache () {}    }
private static class Integercache {        static final int high;        Static final Integer cache[];        static {            final int low = -128;            High value is configured by property            int h = 127;            if (integercachehighpropvalue! = null) {                //use Long.decode. Avoid invoking methods that                //require Integer ' s autoboxing cache to be initialized                int i = Long.decode (integercachehighpropvalue). Intvalue ();                i = Math.max (i, 127);                Maximum array size is integer.max_value                h = math.min (i, Integer.max_value--low);            }            High = h;            cache = new Integer[(high-low) + 1];            int j = Low;            for (int k = 0; k < cache.length; k++)                cache[k] = new Integer (j + +);        }        Private Integercache () {}    }

As you can see from these 2 pieces of code, when you create an integer object by using the ValueOf method, if the value is between [-128,127], a reference to the object that already exists in Integercache.cache is returned, otherwise a new integer object is created.

The values for I1 and I2 in the above code are 100, so the objects that already exist are taken directly from the cache, so i1 and I2 point to the same object, while i3 and I4 point to different objects respectively.

Two pairs of integers are exactly the same, why is one output true and one output false?

Related Article

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.