src/share/classes/java/lang/Long.java

Print this page




 543     private static class LongCache {
 544         private LongCache(){}
 545 
 546         static final Long cache[] = new Long[-(-128) + 127 + 1];
 547 
 548         static {
 549             for(int i = 0; i < cache.length; i++)
 550                 cache[i] = new Long(i - 128);
 551         }
 552     }
 553 
 554     /**
 555      * Returns a {@code Long} instance representing the specified
 556      * {@code long} value.
 557      * If a new {@code Long} instance is not required, this method
 558      * should generally be used in preference to the constructor
 559      * {@link #Long(long)}, as this method is likely to yield
 560      * significantly better space and time performance by caching
 561      * frequently requested values.
 562      *





 563      * @param  l a long value.
 564      * @return a {@code Long} instance representing {@code l}.
 565      * @since  1.5
 566      */
 567     public static Long valueOf(long l) {
 568         final int offset = 128;
 569         if (l >= -128 && l <= 127) { // will cache
 570             return LongCache.cache[(int)l + offset];
 571         }
 572         return new Long(l);
 573     }
 574 
 575     /**
 576      * Decodes a {@code String} into a {@code Long}.
 577      * Accepts decimal, hexadecimal, and octal numbers given by the
 578      * following grammar:
 579      *
 580      * <blockquote>
 581      * <dl>
 582      * <dt><i>DecodableString:</i>




 543     private static class LongCache {
 544         private LongCache(){}
 545 
 546         static final Long cache[] = new Long[-(-128) + 127 + 1];
 547 
 548         static {
 549             for(int i = 0; i < cache.length; i++)
 550                 cache[i] = new Long(i - 128);
 551         }
 552     }
 553 
 554     /**
 555      * Returns a {@code Long} instance representing the specified
 556      * {@code long} value.
 557      * If a new {@code Long} instance is not required, this method
 558      * should generally be used in preference to the constructor
 559      * {@link #Long(long)}, as this method is likely to yield
 560      * significantly better space and time performance by caching
 561      * frequently requested values.
 562      *
 563      * Note that unlike the {@linkplain Integer#valueOf(int)
 564      * corresponding method} in the {@code Integer} class, this method
 565      * is <em>not</em> required to cache values within a particular
 566      * range.
 567      *
 568      * @param  l a long value.
 569      * @return a {@code Long} instance representing {@code l}.
 570      * @since  1.5
 571      */
 572     public static Long valueOf(long l) {
 573         final int offset = 128;
 574         if (l >= -128 && l <= 127) { // will cache
 575             return LongCache.cache[(int)l + offset];
 576         }
 577         return new Long(l);
 578     }
 579 
 580     /**
 581      * Decodes a {@code String} into a {@code Long}.
 582      * Accepts decimal, hexadecimal, and octal numbers given by the
 583      * following grammar:
 584      *
 585      * <blockquote>
 586      * <dl>
 587      * <dt><i>DecodableString:</i>