< 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 wraps a value of the primitive type {@code
  49  * long} in an object. An object of type {@code Long} contains a
  50  * single field whose type is {@code long}.
  51  *
  52  * <p> In addition, this class provides several methods for converting
  53  * a {@code long} to a {@code String} and a {@code String} to a {@code
  54  * long}, as well as other constants and methods useful when dealing
  55  * with a {@code long}.
  56  *
  57  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
  58  * class; programmers should treat instances that are
  59  * {@linkplain #equals(Object) equal} as interchangeable and should not
  60  * use instances for synchronization, or unpredictable behavior may
  61  * occur. For example, in a future release, synchronization may fail.
  62  *
  63  * <p>Implementation note: The implementations of the "bit twiddling"
  64  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  65  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  66  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  67  * Delight</i>, (Addison Wesley, 2002).
  68  *
  69  * @author  Lee Boynton
  70  * @author  Arthur van Hoff
  71  * @author  Josh Bloch
  72  * @author  Joseph D. Darcy
  73  * @since   1.0
  74  */

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

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

 993     public static Long valueOf(long l) {
 994         final int offset = 128;
 995         if (l >= -128 && l <= 127) { // will cache
 996             return LongCache.cache[(int)l + offset];
 997         }
 998         return new Long(l);
 999     }
1000 
1001     /**
1002      * Decodes a {@code String} into a {@code Long}.
1003      * Accepts decimal, hexadecimal, and octal numbers given by the
1004      * following grammar:
1005      *
1006      * <blockquote>
1007      * <dl>
1008      * <dt><i>DecodableString:</i>
1009      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1010      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1011      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1012      * <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 wraps a value of the primitive type {@code
  50  * long} in an object. 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 <i>Hacker's
  68  * Delight</i>, (Addison Wesley, 2002).
  69  *
  70  * @author  Lee Boynton
  71  * @author  Arthur van Hoff
  72  * @author  Josh Bloch
  73  * @author  Joseph D. Darcy
  74  * @since   1.0
  75  */
  76 @jdk.internal.MigratedValueClass
  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     @DeserializeConstructor
 996     public static Long valueOf(long l) {
 997         final int offset = 128;
 998         if (l >= -128 && l <= 127) { // will cache
 999             return LongCache.cache[(int)l + offset];
1000         }
1001         return new Long(l);
1002     }
1003 
1004     /**
1005      * Decodes a {@code String} into a {@code Long}.
1006      * Accepts decimal, hexadecimal, and octal numbers given by the
1007      * following grammar:
1008      *
1009      * <blockquote>
1010      * <dl>
1011      * <dt><i>DecodableString:</i>
1012      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1013      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1014      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1015      * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
< prev index next >