< prev index next >

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

Print this page

  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Native;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.constant.Constable;
  31 import java.lang.constant.ConstantDesc;
  32 import java.math.*;
  33 import java.util.Objects;
  34 import java.util.Optional;
  35 
  36 import jdk.internal.misc.CDS;

  37 import jdk.internal.util.DecimalDigits;
  38 import jdk.internal.vm.annotation.ForceInline;
  39 import jdk.internal.vm.annotation.IntrinsicCandidate;
  40 import jdk.internal.vm.annotation.Stable;
  41 
  42 import static java.lang.Character.digit;
  43 import static java.lang.String.COMPACT_STRINGS;
  44 import static java.lang.String.LATIN1;
  45 import static java.lang.String.UTF16;
  46 
  47 /**
  48  * The {@code Long} class is the {@linkplain
  49  * java.lang##wrapperClass wrapper class} for values of the primitive
  50  * type {@code long}. An object of type {@code Long} contains a
  51  * single field whose type is {@code long}.
  52  *
  53  * <p> In addition, this class provides several methods for converting
  54  * a {@code long} to a {@code String} and a {@code String} to a {@code
  55  * long}, as well as other constants and methods useful when dealing
  56  * with a {@code long}.
  57  *
  58  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
  59  * class; programmers should treat instances that are
  60  * {@linkplain #equals(Object) equal} as interchangeable and should not
  61  * use instances for synchronization, or unpredictable behavior may
  62  * occur. For example, in a future release, synchronization may fail.









  63  *
  64  * <p>Implementation note: The implementations of the "bit twiddling"
  65  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  66  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  67  * based on material from Henry S. Warren, Jr.'s <cite>Hacker's
  68  * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
  69  * Delight, Second Edition</cite>, (Pearson Education, 2013).
  70  *
  71  * @author  Lee Boynton
  72  * @author  Arthur van Hoff
  73  * @author  Josh Bloch
  74  * @author  Joseph D. Darcy
  75  * @since   1.0
  76  */

  77 @jdk.internal.ValueBased
  78 public final class Long extends Number
  79         implements Comparable<Long>, Constable, ConstantDesc {
  80     /**
  81      * A constant holding the minimum value a {@code long} can
  82      * have, -2<sup>63</sup>.
  83      */
  84     @Native public static final long MIN_VALUE = 0x8000000000000000L;
  85 
  86     /**
  87      * A constant holding the maximum value a {@code long} can
  88      * have, 2<sup>63</sup>-1.
  89      */
  90     @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
  91 
  92     /**
  93      * The {@code Class} instance representing the primitive type
  94      * {@code long}.
  95      *
  96      * @since   1.1

 975         }
 976     }
 977 
 978     /**
 979      * Returns a {@code Long} instance representing the specified
 980      * {@code long} value.
 981      * If a new {@code Long} instance is not required, this method
 982      * should generally be used in preference to the constructor
 983      * {@link #Long(long)}, as this method is likely to yield
 984      * significantly better space and time performance by caching
 985      * frequently requested values.
 986      *
 987      * This method will always cache values in the range -128 to 127,
 988      * inclusive, and may cache other values outside of this range.
 989      *
 990      * @param  l a long value.
 991      * @return a {@code Long} instance representing {@code l}.
 992      * @since  1.5
 993      */
 994     @IntrinsicCandidate

 995     public static Long valueOf(long l) {
 996         final int offset = 128;
 997         if (l >= -128 && l <= 127) { // will cache
 998             return LongCache.cache[(int)l + offset];
 999         }
1000         return new Long(l);
1001     }
1002 
1003     /**
1004      * Decodes a {@code String} into a {@code Long}.
1005      * Accepts decimal, hexadecimal, and octal numbers given by the
1006      * following grammar:
1007      *
1008      * <blockquote>
1009      * <dl>
1010      * <dt><i>DecodableString:</i>
1011      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1012      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1013      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1014      * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>

  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.annotation.Native;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.constant.Constable;
  31 import java.lang.constant.ConstantDesc;
  32 import java.math.*;
  33 import java.util.Objects;
  34 import java.util.Optional;
  35 
  36 import jdk.internal.misc.CDS;
  37 import jdk.internal.value.DeserializeConstructor;
  38 import jdk.internal.util.DecimalDigits;
  39 import jdk.internal.vm.annotation.ForceInline;
  40 import jdk.internal.vm.annotation.IntrinsicCandidate;
  41 import jdk.internal.vm.annotation.Stable;
  42 
  43 import static java.lang.Character.digit;
  44 import static java.lang.String.COMPACT_STRINGS;
  45 import static java.lang.String.LATIN1;
  46 import static java.lang.String.UTF16;
  47 
  48 /**
  49  * The {@code Long} class is the {@linkplain
  50  * java.lang##wrapperClass wrapper class} for values of the primitive
  51  * type {@code long}. An object of type {@code Long} contains a
  52  * single field whose type is {@code long}.
  53  *
  54  * <p> In addition, this class provides several methods for converting
  55  * a {@code long} to a {@code String} and a {@code String} to a {@code
  56  * long}, as well as other constants and methods useful when dealing
  57  * with a {@code long}.
  58  *
  59  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
  60  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
  61  * as interchangeable and should not use instances for synchronization, mutexes, or
  62  * with {@linkplain java.lang.ref.Reference object references}.
  63  *
  64  * <div class="preview-block">
  65  *      <div class="preview-comment">
  66  *          When preview features are enabled, {@code Long} is a {@linkplain Class#isValue value class}.
  67  *          Use of value class instances for synchronization, mutexes, or with
  68  *          {@linkplain java.lang.ref.Reference object references} result in
  69  *          {@link IdentityException}.
  70  *      </div>
  71  * </div>
  72  *
  73  *
  74  * <p>Implementation note: The implementations of the "bit twiddling"
  75  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  76  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  77  * based on material from Henry S. Warren, Jr.'s <cite>Hacker's
  78  * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
  79  * Delight, Second Edition</cite>, (Pearson Education, 2013).
  80  *
  81  * @author  Lee Boynton
  82  * @author  Arthur van Hoff
  83  * @author  Josh Bloch
  84  * @author  Joseph D. Darcy
  85  * @since   1.0
  86  */
  87 @jdk.internal.MigratedValueClass
  88 @jdk.internal.ValueBased
  89 public final class Long extends Number
  90         implements Comparable<Long>, Constable, ConstantDesc {
  91     /**
  92      * A constant holding the minimum value a {@code long} can
  93      * have, -2<sup>63</sup>.
  94      */
  95     @Native public static final long MIN_VALUE = 0x8000000000000000L;
  96 
  97     /**
  98      * A constant holding the maximum value a {@code long} can
  99      * have, 2<sup>63</sup>-1.
 100      */
 101     @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
 102 
 103     /**
 104      * The {@code Class} instance representing the primitive type
 105      * {@code long}.
 106      *
 107      * @since   1.1

 986         }
 987     }
 988 
 989     /**
 990      * Returns a {@code Long} instance representing the specified
 991      * {@code long} value.
 992      * If a new {@code Long} instance is not required, this method
 993      * should generally be used in preference to the constructor
 994      * {@link #Long(long)}, as this method is likely to yield
 995      * significantly better space and time performance by caching
 996      * frequently requested values.
 997      *
 998      * This method will always cache values in the range -128 to 127,
 999      * inclusive, and may cache other values outside of this range.
1000      *
1001      * @param  l a long value.
1002      * @return a {@code Long} instance representing {@code l}.
1003      * @since  1.5
1004      */
1005     @IntrinsicCandidate
1006     @DeserializeConstructor
1007     public static Long valueOf(long l) {
1008         final int offset = 128;
1009         if (l >= -128 && l <= 127) { // will cache
1010             return LongCache.cache[(int)l + offset];
1011         }
1012         return new Long(l);
1013     }
1014 
1015     /**
1016      * Decodes a {@code String} into a {@code Long}.
1017      * Accepts decimal, hexadecimal, and octal numbers given by the
1018      * following grammar:
1019      *
1020      * <blockquote>
1021      * <dl>
1022      * <dt><i>DecodableString:</i>
1023      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1024      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1025      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1026      * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
< prev index next >