src/java.base/share/classes/java/lang/Number.java
author igerasim
Thu, 23 Aug 2018 12:09:46 -0700
changeset 51509 758b3f3f3a8d
parent 47216 71c04702a3d5
child 54946 b7c408df5008
permissions -rw-r--r--
8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros() Reviewed-by: martin Contributed-by: ivan.gerasimov@oracle.com, martinrb@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28748
af951d0d2607 8067669: Documentation for methods in Number incomplete regarding too large values.
bpb
parents: 25859
diff changeset
     2
 * Copyright (c) 1994, 2015, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    29
 * The abstract class {@code Number} is the superclass of platform
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    30
 * classes representing numeric values that are convertible to the
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    31
 * primitive types {@code byte}, {@code double}, {@code float}, {@code
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    32
 * int}, {@code long}, and {@code short}.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    33
 *
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    34
 * The specific semantics of the conversion from the numeric value of
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    35
 * a particular {@code Number} implementation to a given primitive
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    36
 * type is defined by the {@code Number} implementation in question.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    37
 *
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    38
 * For platform classes, the conversion is often analogous to a
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    39
 * narrowing primitive conversion or a widening primitive conversion
28748
af951d0d2607 8067669: Documentation for methods in Number incomplete regarding too large values.
bpb
parents: 25859
diff changeset
    40
 * as defined in <cite>The Java&trade; Language Specification</cite>
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    41
 * for converting between primitive types.  Therefore, conversions may
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    42
 * lose information about the overall magnitude of a numeric value, may
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    43
 * lose precision, and may even return a result of a different sign
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    44
 * than the input.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    45
 *
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    46
 * See the documentation of a given {@code Number} implementation for
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    47
 * conversion details.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author      Lee Boynton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author      Arthur van Hoff
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    51
 * @jls 5.1.2 Widening Primitive Conversions
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    52
 * @jls 5.1.3 Narrowing Primitive Conversions
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 10067
diff changeset
    53
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public abstract class Number implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
28748
af951d0d2607 8067669: Documentation for methods in Number incomplete regarding too large values.
bpb
parents: 25859
diff changeset
    57
     * Returns the value of the specified number as an {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @return  the numeric value represented by this object after conversion
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    60
     *          to type {@code int}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public abstract int intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
28748
af951d0d2607 8067669: Documentation for methods in Number incomplete regarding too large values.
bpb
parents: 25859
diff changeset
    65
     * Returns the value of the specified number as a {@code long}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return  the numeric value represented by this object after conversion
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    68
     *          to type {@code long}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public abstract long longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
28748
af951d0d2607 8067669: Documentation for methods in Number incomplete regarding too large values.
bpb
parents: 25859
diff changeset
    73
     * Returns the value of the specified number as a {@code float}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return  the numeric value represented by this object after conversion
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    76
     *          to type {@code float}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public abstract float floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
28748
af951d0d2607 8067669: Documentation for methods in Number incomplete regarding too large values.
bpb
parents: 25859
diff changeset
    81
     * Returns the value of the specified number as a {@code double}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return  the numeric value represented by this object after conversion
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    84
     *          to type {@code double}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public abstract double doubleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
28748
af951d0d2607 8067669: Documentation for methods in Number incomplete regarding too large values.
bpb
parents: 25859
diff changeset
    89
     * Returns the value of the specified number as a {@code byte}.
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    90
     *
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    91
     * <p>This implementation returns the result of {@link #intValue} cast
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    92
     * to a {@code byte}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @return  the numeric value represented by this object after conversion
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
    95
     *          to type {@code byte}.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 10067
diff changeset
    96
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public byte byteValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return (byte)intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
28748
af951d0d2607 8067669: Documentation for methods in Number incomplete regarding too large values.
bpb
parents: 25859
diff changeset
   103
     * Returns the value of the specified number as a {@code short}.
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
   104
     *
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
   105
     * <p>This implementation returns the result of {@link #intValue} cast
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
   106
     * to a {@code short}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return  the numeric value represented by this object after conversion
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 5506
diff changeset
   109
     *          to type {@code short}.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 10067
diff changeset
   110
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public short shortValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return (short)intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static final long serialVersionUID = -8742448824652078965L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}