src/java.base/share/classes/java/lang/Byte.java
author darcy
Thu, 29 Aug 2019 16:31:34 -0700
changeset 57956 e0b8b019d2f5
parent 54952 a978d86ac389
permissions -rw-r--r--
8229997: Apply java.io.Serial annotations in java.base Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54952
a978d86ac389 8224175: Fix inconsistencies in @jls and @jvms tags
darcy
parents: 52626
diff changeset
     2
 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3964
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    28
import jdk.internal.HotSpotIntrinsicCandidate;
52626
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    29
import jdk.internal.misc.VM;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The {@code Byte} class wraps a value of primitive type {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * in an object.  An object of type {@code Byte} contains a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * field whose type is {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>In addition, this class provides several methods for converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * a {@code byte} to a {@code String} and a {@code String} to a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * byte}, as well as other constants and methods useful when dealing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * with a {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Nakul Saraiya
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see     java.lang.Number
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21334
diff changeset
    45
 * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public final class Byte extends Number implements Comparable<Byte> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * A constant holding the minimum value a {@code byte} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * have, -2<sup>7</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final byte   MIN_VALUE = -128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * A constant holding the maximum value a {@code byte} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * have, 2<sup>7</sup>-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final byte   MAX_VALUE = 127;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * The {@code Class} instance representing the primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 10067
diff changeset
    65
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Returns a new {@code String} object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * specified {@code byte}. The radix is assumed to be 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param b the {@code byte} to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @return the string representation of the specified {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @see java.lang.Integer#toString(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static String toString(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return Integer.toString((int)b, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static class ByteCache {
52626
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    81
        private ByteCache() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
52626
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    83
        static final Byte[] cache;
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    84
        static Byte[] archivedCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        static {
52626
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    87
            final int size = -(-128) + 127 + 1;
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    88
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    89
            // Load and use the archived cache if it exists
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    90
            VM.initializeFromArchive(ByteCache.class);
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    91
            if (archivedCache == null || archivedCache.length != size) {
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    92
                Byte[] c = new Byte[size];
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    93
                byte value = (byte)-128;
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    94
                for(int i = 0; i < size; i++) {
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    95
                    c[i] = new Byte(value++);
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    96
                }
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    97
                archivedCache = c;
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    98
            }
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 47216
diff changeset
    99
            cache = archivedCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Returns a {@code Byte} instance representing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * {@code byte} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * If a new {@code Byte} instance is not required, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * should generally be used in preference to the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * {@link #Byte(byte)}, as this method is likely to yield
3224
3aa65803ae07 6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents: 2
diff changeset
   109
     * significantly better space and time performance since
3aa65803ae07 6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents: 2
diff changeset
   110
     * all byte values are cached.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param  b a byte value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return a {@code Byte} instance representing {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   116
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static Byte valueOf(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        final int offset = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return ByteCache.cache[(int)b + offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Parses the string argument as a signed {@code byte} in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * radix specified by the second argument. The characters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * string must all be digits, of the specified radix (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * determined by whether {@link java.lang.Character#digit(char,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * int)} returns a nonnegative value) except that the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * character may be an ASCII minus sign {@code '-'}
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   129
     * ({@code '\u005Cu002D'}) to indicate a negative value or an
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   130
     * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * indicate a positive value.  The resulting {@code byte} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>An exception of type {@code NumberFormatException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * thrown if any of the following situations occurs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <li> The first argument is {@code null} or is a string of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * length zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <li> The radix is either smaller than {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * java.lang.Character#MIN_RADIX} or larger than {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * java.lang.Character#MAX_RADIX}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <li> Any character of the string is not a digit of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * specified radix, except that the first character may be a minus
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   146
     * sign {@code '-'} ({@code '\u005Cu002D'}) or plus sign
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   147
     * {@code '+'} ({@code '\u005Cu002B'}) provided that the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * string is longer than length 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <li> The value represented by the string is not a value of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param s         the {@code String} containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *                  {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *                  representation to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param radix     the radix to be used while parsing {@code s}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return          the {@code byte} value represented by the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *                   argument in the specified radix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws          NumberFormatException If the string does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *                  not contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static byte parseByte(String s, int radix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int i = Integer.parseInt(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (i < MIN_VALUE || i > MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new NumberFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                "Value out of range. Value:\"" + s + "\" Radix:" + radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return (byte)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Parses the string argument as a signed decimal {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * byte}. The characters in the string must all be decimal digits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * except that the first character may be an ASCII minus sign
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   176
     * {@code '-'} ({@code '\u005Cu002D'}) to indicate a negative
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * value or an ASCII plus sign {@code '+'}
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   178
     * ({@code '\u005Cu002B'}) to indicate a positive value. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * resulting {@code byte} value is returned, exactly as if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * argument and the radix 10 were given as arguments to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * #parseByte(java.lang.String, int)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param s         a {@code String} containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *                  {@code byte} representation to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return          the {@code byte} value represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *                  argument in decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @throws          NumberFormatException if the string does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *                  contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static byte parseByte(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return parseByte(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns a {@code Byte} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * extracted from the specified {@code String} when parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * with the radix given by the second argument. The first argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * is interpreted as representing a signed {@code byte} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * the radix specified by the second argument, exactly as if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * argument were given to the {@link #parseByte(java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * int)} method. The result is a {@code Byte} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * represents the {@code byte} value specified by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <p> In other words, this method returns a {@code Byte} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * {@code new Byte(Byte.parseByte(s, radix))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param s         the string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param radix     the radix to be used in interpreting {@code s}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return          a {@code Byte} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *                  represented by the string argument in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *                  specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @throws          NumberFormatException If the {@code String} does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *                  not contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public static Byte valueOf(String s, int radix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        throws NumberFormatException {
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3943
diff changeset
   221
        return valueOf(parseByte(s, radix));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Returns a {@code Byte} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * given by the specified {@code String}. The argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * interpreted as representing a signed decimal {@code byte},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * exactly as if the argument were given to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * #parseByte(java.lang.String)} method. The result is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * {@code Byte} object that represents the {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * value specified by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p> In other words, this method returns a {@code Byte} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * {@code new Byte(Byte.parseByte(s))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param s         the string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @return          a {@code Byte} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *                  represented by the string argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @throws          NumberFormatException If the {@code String} does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *                  not contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public static Byte valueOf(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return valueOf(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Decodes a {@code String} into a {@code Byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Accepts decimal, hexadecimal, and octal numbers given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * the following grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <dt><i>DecodableString:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18546
diff changeset
   263
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <dt><i>Sign:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <dd>{@code -}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <dd>{@code +}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 5506
diff changeset
   271
     * are as defined in section 3.10.1 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 5506
diff changeset
   272
     * <cite>The Java&trade; Language Specification</cite>,
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 5506
diff changeset
   273
     * except that underscores are not accepted between digits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <p>The sequence of characters following an optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * "{@code #}", or leading zero) is parsed as by the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Byte.parseByte} method with the indicated radix (10, 16, or 8).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * This sequence of characters must represent a positive value or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * a {@link NumberFormatException} will be thrown.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * negated if first character of the specified {@code String} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * the minus sign.  No whitespace characters are permitted in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param     nm the {@code String} to decode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @return   a {@code Byte} object holding the {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *          value represented by {@code nm}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @throws  NumberFormatException  if the {@code String} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *            contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @see java.lang.Byte#parseByte(java.lang.String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public static Byte decode(String nm) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int i = Integer.decode(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (i < MIN_VALUE || i > MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new NumberFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    "Value " + i + " out of range from input " + nm);
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3943
diff changeset
   297
        return valueOf((byte)i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * The value of the {@code Byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private final byte value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Constructs a newly allocated {@code Byte} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * represents the specified {@code byte} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param value     the value to be represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *                  {@code Byte}.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   313
     *
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   314
     * @deprecated
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   315
     * It is rarely appropriate to use this constructor. The static factory
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   316
     * {@link #valueOf(byte)} is generally a better choice, as it is
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   317
     * likely to yield significantly better space and time performance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   319
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public Byte(byte value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Constructs a newly allocated {@code Byte} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * represents the {@code byte} value indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * {@code String} parameter. The string is converted to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * {@code byte} value in exactly the manner used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * {@code parseByte} method for radix 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param s         the {@code String} to be converted to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *                  {@code Byte}
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   333
     * @throws          NumberFormatException if the {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *                  does not contain a parsable {@code byte}.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   335
     *
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   336
     * @deprecated
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   337
     * It is rarely appropriate to use this constructor.
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   338
     * Use {@link #parseByte(String)} to convert a string to a
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   339
     * {@code byte} primitive, or use {@link #valueOf(String)}
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   340
     * to convert a string to a {@code Byte} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 33519
diff changeset
   342
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public Byte(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        this.value = parseByte(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns the value of this {@code Byte} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 25859
diff changeset
   351
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public byte byteValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   357
     * Returns the value of this {@code Byte} as a {@code short} after
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   358
     * a widening primitive conversion.
54952
a978d86ac389 8224175: Fix inconsistencies in @jls and @jvms tags
darcy
parents: 52626
diff changeset
   359
     * @jls 5.1.2 Widening Primitive Conversion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public short shortValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return (short)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   366
     * Returns the value of this {@code Byte} as an {@code int} after
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   367
     * a widening primitive conversion.
54952
a978d86ac389 8224175: Fix inconsistencies in @jls and @jvms tags
darcy
parents: 52626
diff changeset
   368
     * @jls 5.1.2 Widening Primitive Conversion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   375
     * Returns the value of this {@code Byte} as a {@code long} after
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   376
     * a widening primitive conversion.
54952
a978d86ac389 8224175: Fix inconsistencies in @jls and @jvms tags
darcy
parents: 52626
diff changeset
   377
     * @jls 5.1.2 Widening Primitive Conversion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public long longValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return (long)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   384
     * Returns the value of this {@code Byte} as a {@code float} after
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   385
     * a widening primitive conversion.
54952
a978d86ac389 8224175: Fix inconsistencies in @jls and @jvms tags
darcy
parents: 52626
diff changeset
   386
     * @jls 5.1.2 Widening Primitive Conversion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public float floatValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return (float)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   393
     * Returns the value of this {@code Byte} as a {@code double}
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
   394
     * after a widening primitive conversion.
54952
a978d86ac389 8224175: Fix inconsistencies in @jls and @jvms tags
darcy
parents: 52626
diff changeset
   395
     * @jls 5.1.2 Widening Primitive Conversion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public double doubleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return (double)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns a {@code String} object representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * {@code Byte}'s value.  The value is converted to signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * decimal representation and returned as a string, exactly as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * the {@code byte} value were given as an argument to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * {@link java.lang.Byte#toString(byte)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return  a string representation of the value of this object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *          base&nbsp;10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public String toString() {
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3943
diff changeset
   412
        return Integer.toString((int)value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
3942
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents: 3288
diff changeset
   416
     * Returns a hash code for this {@code Byte}; equal to the result
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents: 3288
diff changeset
   417
     * of invoking {@code intValue()}.
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents: 3288
diff changeset
   418
     *
685e04a98396 4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents: 3288
diff changeset
   419
     * @return a hash code value for this {@code Byte}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   421
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public int hashCode() {
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   423
        return Byte.hashCode(value);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   424
    }
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   425
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   426
    /**
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   427
     * Returns a hash code for a {@code byte} value; compatible with
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   428
     * {@code Byte.hashCode()}.
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   429
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 14507
diff changeset
   430
     * @param value the value to hash
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 14507
diff changeset
   431
     * @return a hash code value for a {@code byte} value.
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   432
     * @since 1.8
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   433
     */
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11676
diff changeset
   434
    public static int hashCode(byte value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Compares this object to the specified object.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * {@code true} if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * {@code null} and is a {@code Byte} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * contains the same {@code byte} value as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param obj       the object to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @return          {@code true} if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *                  {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (obj instanceof Byte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return value == ((Byte)obj).byteValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Compares two {@code Byte} objects numerically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param   anotherByte   the {@code Byte} to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @return  the value {@code 0} if this {@code Byte} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *          equal to the argument {@code Byte}; a value less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *          {@code 0} if this {@code Byte} is numerically less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *          than the argument {@code Byte}; and a value greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *           {@code 0} if this {@code Byte} is numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *           greater than the argument {@code Byte} (signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *           comparison).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public int compareTo(Byte anotherByte) {
3943
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   469
        return compare(this.value, anotherByte.value);
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   470
    }
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   471
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   472
    /**
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   473
     * Compares two {@code byte} values numerically.
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   474
     * The value returned is identical to what would be returned by:
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   475
     * <pre>
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   476
     *    Byte.valueOf(x).compareTo(Byte.valueOf(y))
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   477
     * </pre>
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   478
     *
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   479
     * @param  x the first {@code byte} to compare
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   480
     * @param  y the second {@code byte} to compare
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   481
     * @return the value {@code 0} if {@code x == y};
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   482
     *         a value less than {@code 0} if {@code x < y}; and
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   483
     *         a value greater than {@code 0} if {@code x > y}
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   484
     * @since 1.7
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   485
     */
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   486
    public static int compare(byte x, byte y) {
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3942
diff changeset
   487
        return x - y;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
33519
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   491
     * Compares two {@code byte} values numerically treating the values
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   492
     * as unsigned.
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   493
     *
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   494
     * @param  x the first {@code byte} to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   495
     * @param  y the second {@code byte} to compare
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   496
     * @return the value {@code 0} if {@code x == y}; a value less
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   497
     *         than {@code 0} if {@code x < y} as unsigned values; and
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   498
     *         a value greater than {@code 0} if {@code x > y} as
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   499
     *         unsigned values
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   500
     * @since 9
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   501
     */
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   502
    public static int compareUnsigned(byte x, byte y) {
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   503
        return Byte.toUnsignedInt(x) - Byte.toUnsignedInt(y);
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   504
    }
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   505
a33d1c19cbc8 8033148: Lexicographic comparators for arrays
psandoz
parents: 31671
diff changeset
   506
    /**
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   507
     * Converts the argument to an {@code int} by an unsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   508
     * conversion.  In an unsigned conversion to an {@code int}, the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   509
     * high-order 24 bits of the {@code int} are zero and the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   510
     * low-order 8 bits are equal to the bits of the {@code byte} argument.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   511
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   512
     * Consequently, zero and positive {@code byte} values are mapped
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   513
     * to a numerically equal {@code int} value and negative {@code
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   514
     * byte} values are mapped to an {@code int} value equal to the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   515
     * input plus 2<sup>8</sup>.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   516
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   517
     * @param  x the value to convert to an unsigned {@code int}
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   518
     * @return the argument converted to {@code int} by an unsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   519
     *         conversion
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   520
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   521
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   522
    public static int toUnsignedInt(byte x) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   523
        return ((int) x) & 0xff;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   524
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   525
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   526
    /**
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   527
     * Converts the argument to a {@code long} by an unsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   528
     * conversion.  In an unsigned conversion to a {@code long}, the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   529
     * high-order 56 bits of the {@code long} are zero and the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   530
     * low-order 8 bits are equal to the bits of the {@code byte} argument.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   531
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   532
     * Consequently, zero and positive {@code byte} values are mapped
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   533
     * to a numerically equal {@code long} value and negative {@code
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   534
     * byte} values are mapped to a {@code long} value equal to the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   535
     * input plus 2<sup>8</sup>.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   536
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   537
     * @param  x the value to convert to an unsigned {@code long}
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   538
     * @return the argument converted to {@code long} by an unsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   539
     *         conversion
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   540
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   541
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   542
    public static long toUnsignedLong(byte x) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   543
        return ((long) x) & 0xffL;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   544
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   545
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   546
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   547
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * The number of bits used to represent a {@code byte} value in two's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * complement binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    public static final int SIZE = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
14507
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   555
    /**
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   556
     * The number of bytes used to represent a {@code byte} value in two's
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   557
     * complement binary form.
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   558
     *
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   559
     * @since 1.8
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   560
     */
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   561
    public static final int BYTES = SIZE / Byte.SIZE;
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
   562
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /** use serialVersionUID from JDK 1.1. for interoperability */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54952
diff changeset
   564
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    private static final long serialVersionUID = -7183698231559129828L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
}