1 /* 2 * Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 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 /** 29 * The abstract class {@code Number} is the superclass of platform 30 * classes representing numeric values that are convertible to the 31 * primitive types {@code byte}, {@code double}, {@code float}, {@code 32 * int}, {@code long}, and {@code short}. 33 * 34 * The specific semantics of the conversion from the numeric value of 35 * a particular {@code Number} implementation to a given primitive 36 * type is defined by the {@code Number} implementation in question. 37 * 38 * For platform classes, the conversion is often analogous to a 39 * narrowing primitive conversion or a widening primitive conversion 40 * as defined in <cite>The Java Language Specification</cite> 41 * for converting between primitive types. Therefore, conversions may 42 * lose information about the overall magnitude of a numeric value, may 43 * lose precision, and may even return a result of a different sign 44 * than the input. 45 * 46 * See the documentation of a given {@code Number} implementation for 47 * conversion details. 48 * 49 * @author Lee Boynton 50 * @author Arthur van Hoff 51 * @jls 5.1.2 Widening Primitive Conversion 52 * @jls 5.1.3 Narrowing Primitive Conversion 53 * @since 1.0 54 */ 55 @jdk.internal.MigratedValueClass 56 public abstract class Number implements java.io.Serializable { 57 /** 58 * Constructor for subclasses to call. 59 */ 60 public Number() {super();} 61 62 /** 63 * Returns the value of the specified number as an {@code int}. 64 * 65 * @return the numeric value represented by this object after conversion 66 * to type {@code int}. 67 */ 68 public abstract int intValue(); 69 70 /** 71 * Returns the value of the specified number as a {@code long}. 72 * 73 * @return the numeric value represented by this object after conversion 74 * to type {@code long}. 75 */ 76 public abstract long longValue(); 77 78 /** 79 * Returns the value of the specified number as a {@code float}. 80 * 81 * @return the numeric value represented by this object after conversion 82 * to type {@code float}. 83 */ 84 public abstract float floatValue(); 85 86 /** 87 * Returns the value of the specified number as a {@code double}. 88 * 89 * @return the numeric value represented by this object after conversion 90 * to type {@code double}. 91 */ 92 public abstract double doubleValue(); 93 94 /** 95 * Returns the value of the specified number as a {@code byte}. 96 * 97 * @implSpec 98 * The default implementation returns the result of {@link #intValue} cast 99 * to a {@code byte}. 100 * 101 * @return the numeric value represented by this object after conversion 102 * to type {@code byte}. 103 * @since 1.1 104 */ 105 public byte byteValue() { 106 return (byte)intValue(); 107 } 108 109 /** 110 * Returns the value of the specified number as a {@code short}. 111 * 112 * @implSpec 113 * The default implementation returns the result of {@link #intValue} cast 114 * to a {@code short}. 115 * 116 * @return the numeric value represented by this object after conversion 117 * to type {@code short}. 118 * @since 1.1 119 */ 120 public short shortValue() { 121 return (short)intValue(); 122 } 123 124 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 125 @java.io.Serial 126 private static final long serialVersionUID = -8742448824652078965L; 127 }