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 * @since 1.0
72 */
73 @jdk.internal.ValueBased
74 public final class Long extends Number
75 implements Comparable<Long>, Constable, ConstantDesc {
76 /**
77 * A constant holding the minimum value a {@code long} can
78 * have, -2<sup>63</sup>.
79 */
80 @Native public static final long MIN_VALUE = 0x8000000000000000L;
81
82 /**
83 * A constant holding the maximum value a {@code long} can
84 * have, 2<sup>63</sup>-1.
85 */
86 @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
87
88 /**
89 * The {@code Class} instance representing the primitive type
90 * {@code long}.
91 *
92 * @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.ForceInline;
41 import jdk.internal.vm.annotation.IntrinsicCandidate;
42 import jdk.internal.vm.annotation.Stable;
43
44 import static java.lang.Character.digit;
45 import static java.lang.String.COMPACT_STRINGS;
46 import static java.lang.String.LATIN1;
47 import static java.lang.String.UTF16;
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
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 -}
|