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.AOTSafeClassInitializer;
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
46 /**
47 * The {@code Long} class is the {@linkplain
48 * java.lang##wrapperClass wrapper class} for values of the primitive
49 * type {@code long}. 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 <cite>Hacker's
67 * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
68 * Delight, Second Edition</cite>, (Pearson Education, 2013).
69 *
70 * @since 1.0
71 */
72 @jdk.internal.ValueBased
73 public final class Long extends Number
74 implements Comparable<Long>, Constable, ConstantDesc {
75 /**
76 * A constant holding the minimum value a {@code long} can
77 * have, -2<sup>63</sup>.
78 */
79 @Native public static final long MIN_VALUE = 0x8000000000000000L;
80
81 /**
82 * A constant holding the maximum value a {@code long} can
83 * have, 2<sup>63</sup>-1.
84 */
85 @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
86
87 /**
88 * The {@code Class} instance representing the primitive type
89 * {@code long}.
90 *
91 * @since 1.1
904 * @param s the string to be parsed.
905 * @return a {@code Long} object holding the value
906 * represented by the string argument.
907 * @throws NumberFormatException If the string cannot be parsed
908 * as a {@code long}.
909 */
910 public static Long valueOf(String s) throws NumberFormatException
911 {
912 return Long.valueOf(parseLong(s, 10));
913 }
914
915 @AOTSafeClassInitializer
916 private static final class LongCache {
917 private LongCache() {}
918
919 @Stable
920 static final Long[] cache;
921 static Long[] archivedCache;
922
923 static {
924 int size = -(-128) + 127 + 1;
925
926 // Load and use the archived cache if it exists
927 CDS.initializeFromArchive(LongCache.class);
928 if (archivedCache == null) {
929 Long[] c = new Long[size];
930 long value = -128;
931 for(int i = 0; i < size; i++) {
932 c[i] = new Long(value++);
933 }
934 archivedCache = c;
935 }
936 cache = archivedCache;
937 assert cache.length == size;
938 }
939 }
940
941 /**
942 * Returns a {@code Long} instance representing the specified
943 * {@code long} value.
944 * If a new {@code Long} instance is not required, this method
945 * should generally be used in preference to the constructor
946 * {@link #Long(long)}, as this method is likely to yield
947 * significantly better space and time performance by caching
948 * frequently requested values.
949 *
950 * This method will always cache values in the range -128 to 127,
951 * inclusive, and may cache other values outside of this range.
952 *
953 * @param l a long value.
954 * @return a {@code Long} instance representing {@code l}.
955 * @since 1.5
956 */
957 @IntrinsicCandidate
958 public static Long valueOf(long l) {
959 final int offset = 128;
960 if (l >= -128 && l <= 127) { // will cache
961 return LongCache.cache[(int)l + offset];
962 }
963 return new Long(l);
964 }
965
966 /**
967 * Decodes a {@code String} into a {@code Long}.
968 * Accepts decimal, hexadecimal, and octal numbers given by the
969 * following grammar:
970 *
971 * <blockquote>
972 * <dl>
973 * <dt><i>DecodableString:</i>
974 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
975 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
976 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
977 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
978 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
979 *
980 * <dt><i>Sign:</i>
981 * <dd>{@code -}
|
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.misc.PreviewFeatures;
38 import jdk.internal.value.DeserializeConstructor;
39 import jdk.internal.util.DecimalDigits;
40 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
41 import jdk.internal.vm.annotation.ForceInline;
42 import jdk.internal.vm.annotation.IntrinsicCandidate;
43 import jdk.internal.vm.annotation.Stable;
44
45 import static java.lang.Character.digit;
46 import static java.lang.String.COMPACT_STRINGS;
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 * @since 1.0
82 */
83 @jdk.internal.MigratedValueClass
84 @jdk.internal.ValueBased
85 public final class Long extends Number
86 implements Comparable<Long>, Constable, ConstantDesc {
87 /**
88 * A constant holding the minimum value a {@code long} can
89 * have, -2<sup>63</sup>.
90 */
91 @Native public static final long MIN_VALUE = 0x8000000000000000L;
92
93 /**
94 * A constant holding the maximum value a {@code long} can
95 * have, 2<sup>63</sup>-1.
96 */
97 @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
98
99 /**
100 * The {@code Class} instance representing the primitive type
101 * {@code long}.
102 *
103 * @since 1.1
916 * @param s the string to be parsed.
917 * @return a {@code Long} object holding the value
918 * represented by the string argument.
919 * @throws NumberFormatException If the string cannot be parsed
920 * as a {@code long}.
921 */
922 public static Long valueOf(String s) throws NumberFormatException
923 {
924 return Long.valueOf(parseLong(s, 10));
925 }
926
927 @AOTSafeClassInitializer
928 private static final class LongCache {
929 private LongCache() {}
930
931 @Stable
932 static final Long[] cache;
933 static Long[] archivedCache;
934
935 static {
936 if (!PreviewFeatures.isEnabled()) {
937 int size = -(-128) + 127 + 1;
938
939 // Load and use the archived cache if it exists
940 CDS.initializeFromArchive(LongCache.class);
941 if (archivedCache == null) {
942 Long[] c = new Long[size];
943 long value = -128;
944 for(int i = 0; i < size; i++) {
945 c[i] = new Long(value++);
946 }
947 archivedCache = c;
948 }
949 cache = archivedCache;
950 assert cache.length == size;
951 } else {
952 cache = null;
953 assert archivedCache == null;
954 }
955 }
956 }
957
958 /**
959 * Returns a {@code Long} instance representing the specified
960 * {@code long} value.
961 * <div class="preview-block">
962 * <div class="preview-comment">
963 * <p>
964 * - When preview features are NOT enabled, {@code Long} is an identity class.
965 * If a new {@code Long} instance is not required, this method
966 * should generally be used in preference to the constructor
967 * {@link #Long(long)}, as this method is likely to yield
968 * significantly better space and time performance by caching
969 * frequently requested values.
970 * This method will always cache values in the range -128 to 127,
971 * inclusive, and may cache other values outside of this range.
972 * </p>
973 * <p>
974 * - When preview features are enabled, {@code Long} is a {@linkplain Class#isValue value class}.
975 * The {@code valueOf} behavior is the same as invoking the constructor,
976 * whether cached or not.
977 * </p>
978 * </div>
979 * </div>
980 *
981 * @param l a long value.
982 * @return a {@code Long} instance representing {@code l}.
983 * @since 1.5
984 */
985 @IntrinsicCandidate
986 @DeserializeConstructor
987 public static Long valueOf(long l) {
988 if (!PreviewFeatures.isEnabled()) {
989 if (l >= -128 && l <= 127) { // will cache
990 final int offset = 128;
991 return LongCache.cache[(int) l + offset];
992 }
993 }
994 return new Long(l);
995 }
996
997 /**
998 * Decodes a {@code String} into a {@code Long}.
999 * Accepts decimal, hexadecimal, and octal numbers given by the
1000 * following grammar:
1001 *
1002 * <blockquote>
1003 * <dl>
1004 * <dt><i>DecodableString:</i>
1005 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1006 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1007 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1008 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1009 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
1010 *
1011 * <dt><i>Sign:</i>
1012 * <dd>{@code -}
|