Java integer type Comparison

Source: Internet
Author: User

Today I did a topic as follows:

Integer A=10;integer b=10; System.out.print (a==b); Integer C=200;integer d=200; System.out.print (C==d);

Please say the output:

The answer is: true,false

Isn't it weird?

Turn the source to haha;

View Integer.value (int i) method

    public static Integer valueOf (int i) {if        (i >= integercache.low && i <= integercache.high)//judged if greater than low or Less than high            return integercache.cache[i + (-integercache.low)];//out of this cache returns the return new        Integer (i);    }

View Internal Classes

 private static class Integercache {static final int low =-128;        static final int high;        Static final Integer cache[];            static {//high value is configured by property int h = 127; String Integercachehighpropvalue = Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high");//Get JVM parameter if (integercachehighpropvalue! = null) {try {int i = parseint (integer                    Cachehighpropvalue); i = Math.max (i, 127);//Take both maximum//Maximum array size is integer.max_value h = math.mi                    N (I, Integer.max_value-(-low)-1);//fear that the virtual machine array is too large to determine the minimum value of the two} catch (NumberFormatException nfe) {                If The property cannot is parsed into an int, ignore it.            }} high = h;            cache = new Integer[(high-low) + 1];//Create array int j = low; for (int k = 0;K < Cache.length; k++) Cache[k] = new Integer (j + +);//First step to create this object array//range [ -128, 127] must be interned (JLS7 5 .1.7) Assert Integercache.high >= 127;//debug assert} private Integercache () {}}

You can see that you can set this maximum or minimum buffer by setting the Java.lang.Integer.IntegerCache.high of the JVM's parameters, but it will cause the size of the cache array of this Integer to become larger

When the parameter is not set, when the integer is between 128 and 127, the object is removed from the cache array, so the comparison is the same.

The above-mentioned questions can also be explained.

Java integer type Comparison

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.