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
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 -}
1056 /**
1057 * The value of the {@code Long}.
1058 *
1059 * @serial
1060 */
1061 private final long value;
1062
1063 /**
1064 * Constructs a newly allocated {@code Long} object that
1065 * represents the specified {@code long} argument.
1066 *
1067 * @param value the value to be represented by the
1068 * {@code Long} object.
1069 *
1070 * @deprecated
1071 * It is rarely appropriate to use this constructor. The static factory
1072 * {@link #valueOf(long)} is generally a better choice, as it is
1073 * likely to yield significantly better space and time performance.
1074 */
1075 @Deprecated(since="9")
1076 public Long(long value) {
1077 this.value = value;
1078 }
1079
1080 /**
1081 * Constructs a newly allocated {@code Long} object that
1082 * represents the {@code long} value indicated by the
1083 * {@code String} parameter. The string is converted to a
1084 * {@code long} value in exactly the manner used by the
1085 * {@code parseLong} method for radix 10.
1086 *
1087 * @param s the {@code String} to be converted to a
1088 * {@code Long}.
1089 * @throws NumberFormatException if the {@code String} does not
1090 * contain a parsable {@code long}.
1091 *
1092 * @deprecated
1093 * It is rarely appropriate to use this constructor.
1094 * Use {@link #parseLong(String)} to convert a string to a
1095 * {@code long} primitive, or use {@link #valueOf(String)}
|
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.value.ValueClass;
40 import jdk.internal.util.DecimalDigits;
41 import jdk.internal.vm.annotation.AOTSafeClassInitializer;
42 import jdk.internal.vm.annotation.ForceInline;
43 import jdk.internal.vm.annotation.IntrinsicCandidate;
44 import jdk.internal.vm.annotation.Stable;
45
46 import static java.lang.Character.digit;
47 import static java.lang.String.COMPACT_STRINGS;
48
49 /**
50 * The {@code Long} class is the {@linkplain
51 * java.lang##wrapperClass wrapper class} for values of the primitive
52 * type {@code long}. An object of type {@code Long} contains a
53 * single field whose type is {@code long}.
54 *
55 * <p> In addition, this class provides several methods for converting
56 * a {@code long} to a {@code String} and a {@code String} to a {@code
57 * long}, as well as other constants and methods useful when dealing
58 * with a {@code long}.
59 *
60 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
61 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
62 * as interchangeable and should not use instances for synchronization, mutexes, or
63 * with {@linkplain java.lang.ref.Reference object references}.
64 *
65 * <div class="preview-block">
66 * <div class="preview-comment">
67 * When preview features are enabled, {@code Long} is a {@linkplain Class#isValue value class}.
68 * Use of value class instances for synchronization, mutexes, or with
69 * {@linkplain java.lang.ref.Reference object references} result in
70 * {@link IdentityException}.
71 * </div>
72 * </div>
73 *
74 *
75 * <p>Implementation note: The implementations of the "bit twiddling"
76 * methods (such as {@link #highestOneBit(long) highestOneBit} and
77 * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
78 * based on material from Henry S. Warren, Jr.'s <cite>Hacker's
79 * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
80 * Delight, Second Edition</cite>, (Pearson Education, 2013).
81 *
82 * @since 1.0
83 */
84 @jdk.internal.MigratedValueClass
85 @jdk.internal.ValueBased
86 public final class Long extends Number
87 implements Comparable<Long>, Constable, ConstantDesc {
88 /**
89 * A constant holding the minimum value a {@code long} can
90 * have, -2<sup>63</sup>.
91 */
92 @Native public static final long MIN_VALUE = 0x8000000000000000L;
93
94 /**
95 * A constant holding the maximum value a {@code long} can
96 * have, 2<sup>63</sup>-1.
97 */
98 @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
99
100 /**
101 * The {@code Class} instance representing the primitive type
102 * {@code long}.
103 *
104 * @since 1.1
922 */
923 public static Long valueOf(String s) throws NumberFormatException
924 {
925 return Long.valueOf(parseLong(s, 10));
926 }
927
928 @AOTSafeClassInitializer
929 private static final class LongCache {
930 private LongCache() {}
931
932 @Stable
933 static final Long[] cache;
934 static Long[] archivedCache;
935
936 static {
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 = newCacheArray(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 }
952
953 private static Long[] newCacheArray(int size) {
954 // ValueClass.newReferenceArray requires a value class component.
955 if (PreviewFeatures.isEnabled()) {
956 return (Long[]) ValueClass.newReferenceArray(Long.class, size);
957 }
958 return new Long[size];
959 }
960 }
961
962 /**
963 * Returns a {@code Long} instance representing the specified
964 * {@code long} value.
965 * <div class="preview-block">
966 * <div class="preview-comment">
967 * <p>
968 * - When preview features are NOT enabled, {@code Long} is an identity class.
969 * If a new {@code Long} instance is not required, this method
970 * should generally be used in preference to the constructor
971 * {@link #Long(long)}, as this method is likely to yield
972 * significantly better space and time performance by caching
973 * frequently requested values.
974 * This method will always cache values in the range -128 to 127,
975 * inclusive, and may cache other values outside of this range.
976 * </p>
977 * <p>
978 * - When preview features are enabled, {@code Long} is a {@linkplain Class#isValue value class}.
979 * The {@code valueOf} behavior is the same as invoking the constructor,
980 * whether cached or not.
981 * </p>
982 * </div>
983 * </div>
984 *
985 * @param l a long value.
986 * @return a {@code Long} instance representing {@code l}.
987 * @since 1.5
988 */
989 @IntrinsicCandidate
990 public static Long valueOf(long l) {
991 if (l >= -128 && l <= 127) { // will cache
992 final int offset = 128;
993 return LongCache.cache[(int) l + offset];
994 }
995 return new Long(l);
996 }
997
998 /**
999 * Decodes a {@code String} into a {@code Long}.
1000 * Accepts decimal, hexadecimal, and octal numbers given by the
1001 * following grammar:
1002 *
1003 * <blockquote>
1004 * <dl>
1005 * <dt><i>DecodableString:</i>
1006 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1007 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1008 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1009 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1010 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
1011 *
1012 * <dt><i>Sign:</i>
1013 * <dd>{@code -}
1088 /**
1089 * The value of the {@code Long}.
1090 *
1091 * @serial
1092 */
1093 private final long value;
1094
1095 /**
1096 * Constructs a newly allocated {@code Long} object that
1097 * represents the specified {@code long} argument.
1098 *
1099 * @param value the value to be represented by the
1100 * {@code Long} object.
1101 *
1102 * @deprecated
1103 * It is rarely appropriate to use this constructor. The static factory
1104 * {@link #valueOf(long)} is generally a better choice, as it is
1105 * likely to yield significantly better space and time performance.
1106 */
1107 @Deprecated(since="9")
1108 @DeserializeConstructor
1109 public Long(long value) {
1110 this.value = value;
1111 }
1112
1113 /**
1114 * Constructs a newly allocated {@code Long} object that
1115 * represents the {@code long} value indicated by the
1116 * {@code String} parameter. The string is converted to a
1117 * {@code long} value in exactly the manner used by the
1118 * {@code parseLong} method for radix 10.
1119 *
1120 * @param s the {@code String} to be converted to a
1121 * {@code Long}.
1122 * @throws NumberFormatException if the {@code String} does not
1123 * contain a parsable {@code long}.
1124 *
1125 * @deprecated
1126 * It is rarely appropriate to use this constructor.
1127 * Use {@link #parseLong(String)} to convert a string to a
1128 * {@code long} primitive, or use {@link #valueOf(String)}
|