Cache for Java integer classes

Source: Internet
Author: User

First look at a piece of code (using JDK 5), as follows:

[HTML]View PlainCopy
  1. public class Hello
  2. {
  3. public static void Main (string[] args)
  4. {
  5. int A = + , B = 1000;
  6. System.out.println (a = = b);
  7. Integer C = N, d = 1000;
  8. System.out.println (c = = d);
  9. Integer e = + , f = 100;
  10. System.out.println (E = = f);
  11. }
  12. }

Output Result:

[HTML]View PlainCopy
    1. True
    2. False
    3. True

the Java Language specification, 3rd Edition wrote:

[HTML]View PlainCopy
    1. To save memory, for two instances of the following wrapper objects, when their base values are the same, they are always = =:
    2. Boolean
    3. Byte
    4. Character, \u0000-\u007f (7f is the decimal 127)
    5. Integer,-128-127

View the JDK source code as follows:

[Java]View PlainCopy
  1. /**
  2. * Cache to support the object identity semantics of autoboxing for values between
  3. * -128 and 127 (inclusive) as required by JLS.
  4. *
  5. * The cache is initialized on first usage. During VM Initialization the
  6. * Getandremovecacheproperties method May is used to get and remove any system
  7. * Properites that configure the cache size. At this time, the size of the
  8. * Cache May is controlled by the VM Option-xx:autoboxcachemax=<size>.
  9. */
  10. //value of Java.lang.Integer.IntegerCache.high property (obtained during VM init)
  11. private static String Integercachehighpropvalue;
  12. static void Getandremovecacheproperties () {
  13. if (!sun.misc.vm.isbooted ()) {
  14. Properties props = System.getproperties ();
  15. Integercachehighpropvalue =
  16. (String) Props.remove ("Java.lang.Integer.IntegerCache.high");
  17. if (integercachehighpropvalue! = null)
  18. System.setproperties (props); //Remove from System props
  19. }
  20. }
  21. private static class Integercache {
  22. static final int high;
  23. static final Integer cache[];
  24. Static {
  25. final int low =-128;
  26. //High value is configured by property
  27. int h = 127;
  28. if (integercachehighpropvalue! = null) {
  29. //Use Long.decode This avoid invoking methods that
  30. //Require Integer ' s autoboxing cache to be initialized
  31. int i = Long.decode (integercachehighpropvalue). Intvalue ();
  32. i = Math.max (i, 127);
  33. //Maximum array size is Integer.max_value
  34. h = math.min (i, Integer.max_value--low);
  35. }
  36. High = h;
  37. cache = New integer[(high-low) + 1];
  38. int j = Low;
  39. For (int k = 0; k < cache.length; k++) //Buffer room data
  40. CACHE[K] = new Integer (j + +);
  41. }
  42. Private Integercache () {}
  43. }
  44. /** 
  45. * Returns A <tt>Integer</tt> instance representing the specified
  46. * <tt>int</tt> value.
  47. * If A new <tt>Integer</tt> instance isn't required, this method
  48. * Should generally be used on preference to the constructor
  49. * {@link #Integer (int)}, as this method was likely to yield
  50. * Significantly better space and time performance by caching
  51. * Frequently requested values.
  52. *
  53. * @param i an <code>int</code> value.
  54. * @return A <tt>Integer</tt> instance representing <tt>i</tt>.
  55. * @since 1.5
  56. */
  57. public static Integer valueOf (int i) {
  58. if (i >=-&& i <= integercache.high)
  59. return integercache.cache[i + 128];
  60. Else
  61. return new Integer (i);
  62. }

The integercache here has a static integer array that, when the class loads, creates a 128 to 127 integer object and saves it in the cache array once the program calls the ValueOf method, if the value of I is 128 to 127 The integer object is taken directly between the cache buffer array.

And look at the other wrappers:

    • Boolean: (full cache)
    • Byte: (Cache All)
    • Character (<= 127 cache)
    • Short (-128-127 cache)
    • Long (-128-127 cache)
    • Float (no cache)
    • Doulbe (no cache)

Also for the garbage collector:

[Java]View PlainCopy
    1. Integer i = 100;
    2. i = null;   Would not do any of the object available for GC at all.

The code here does not have objects that conform to the garbage collector's condition, where I is given null, but it points to an integer object in the cache, and the cache is not assigned null, so an integer (100) object is still present.

If I is greater than 127 or less than-128, the object it points to will be eligible for garbage collection:

[Java]View PlainCopy
    1. Integer i = 10000;
    2. i = null;   Would make the newly created an Integer object available for GC.

Cache for Java integer classes (RPM)

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.