< 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
  61  * {@linkplain #equals(Object) equal} as interchangeable and should not
  62  * use instances for synchronization, or unpredictable behavior may
  63  * occur. For example, in a future release, synchronization may fail.
  64  *
  65  * <p>Implementation note: The implementations of the "bit twiddling"
  66  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  67  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  68  * based on material from Henry S. Warren, Jr.'s <cite>Hacker's
  69  * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
  70  * Delight, Second Edition</cite>, (Pearson Education, 2013).
  71  *
  72  * @author  Lee Boynton
  73  * @author  Arthur van Hoff
  74  * @author  Josh Bloch
  75  * @author  Joseph D. Darcy
  76  * @since   1.0
  77  */
  78 @jdk.internal.MigratedValueClass
  79 @jdk.internal.ValueBased
  80 public final class Long extends Number
  81         implements Comparable<Long>, Constable, ConstantDesc {
  82     /**
  83      * A constant holding the minimum value a {@code long} can
  84      * have, -2<sup>63</sup>.
  85      */
  86     @Native public static final long MIN_VALUE = 0x8000000000000000L;
  87 
  88     /**
  89      * A constant holding the maximum value a {@code long} can
  90      * have, 2<sup>63</sup>-1.
  91      */
  92     @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
  93 
  94     /**
  95      * The {@code Class} instance representing the primitive type
  96      * {@code long}.
  97      *
  98      * @since   1.1

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