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
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
936 int size = -(-128) + 127 + 1;
937
938 // Load and use the archived cache if it exists
939 CDS.initializeFromArchive(LongCache.class);
940 if (archivedCache == null) {
941 Long[] c = new Long[size];
942 long value = -128;
943 for(int i = 0; i < size; i++) {
944 c[i] = new Long(value++);
945 }
946 archivedCache = c;
947 }
948 cache = archivedCache;
949 assert cache.length == size;
950 }
951 }
952
953 /**
954 * Returns a {@code Long} instance representing the specified
955 * {@code long} value.
956 * <div class="preview-block">
957 * <div class="preview-comment">
958 * <p>
959 * - When preview features are NOT enabled, {@code Long} is an identity class.
960 * If a new {@code Long} instance is not required, this method
961 * should generally be used in preference to the constructor
962 * {@link #Long(long)}, as this method is likely to yield
963 * significantly better space and time performance by caching
964 * frequently requested values.
965 * This method will always cache values in the range -128 to 127,
966 * inclusive, and may cache other values outside of this range.
967 * </p>
968 * <p>
969 * - When preview features are enabled, {@code Long} is a {@linkplain Class#isValue value class}.
970 * The {@code valueOf} behavior is the same as invoking the constructor,
971 * whether cached or not.
972 * </p>
973 * </div>
974 * </div>
975 *
976 * @param l a long value.
977 * @return a {@code Long} instance representing {@code l}.
978 * @since 1.5
979 */
980 @IntrinsicCandidate
981 @DeserializeConstructor
982 public static Long valueOf(long l) {
983 if (!PreviewFeatures.isEnabled()) {
984 if (l >= -128 && l <= 127) { // will cache
985 final int offset = 128;
986 return LongCache.cache[(int) l + offset];
987 }
988 }
989 return new Long(l);
990 }
991
992 /**
993 * Decodes a {@code String} into a {@code Long}.
994 * Accepts decimal, hexadecimal, and octal numbers given by the
995 * following grammar:
996 *
997 * <blockquote>
998 * <dl>
999 * <dt><i>DecodableString:</i>
1000 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1001 * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1002 * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1003 * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1004 * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
1005 *
1006 * <dt><i>Sign:</i>
1007 * <dd>{@code -}
|