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 * The abstract class {@code Number} is the superclass of platform 29 * classes representing numeric values that are convertible to the 30 * primitive types {@code byte}, {@code double}, {@code float}, {@code 31 * int}, {@code long}, and {@code short}. 32 * 33 * The specific semantics of the conversion from the numeric value of 34 * a particular {@code Number} implementation to a given primitive 35 * type is defined by the {@code Number} implementation in question. 36 * 37 * For platform classes, the conversion is often analogous to a 38 * narrowing primitive conversion or a widening primitive conversion 39 * as defined in <cite>The Java Language Specification</cite> 40 * for converting between primitive types. Therefore, conversions may 41 * lose information about the overall magnitude of a numeric value, may 42 * lose precision, and may even return a result of a different sign 43 * than the input. 44 * 45 * See the documentation of a given {@code Number} implementation for 46 * conversion details. 47 * 48 * <div class="preview-block"> 49 * <div class="preview-comment"> 50 * When preview features are enabled, {@code Number} is 51 * an abstract {@linkplain Class#isValue value class}. 52 * Subclasses of {@code Number} can be either an {@linkplain Class#isIdentity identity class} 53 * or a {@linkplain Class#isValue value class}. 54 * See {@jls The Java Language Specification 8.1.1.5 Value Classes}. 55 * </div> 56 * </div> 57 * 58 * @author Lee Boynton 59 * @author Arthur van Hoff 60 * @jls 5.1.2 Widening Primitive Conversion 61 * @jls 5.1.3 Narrowing Primitive Conversion 62 * @since 1.0 63 */ 64 @jdk.internal.MigratedValueClass 65 public abstract class Number implements java.io.Serializable { 66 /** 67 * Constructor for subclasses to call. 68 */ 69 public Number() {super();} 70 71 /** 72 * Returns the value of the specified number as an {@code int}. 73 * 74 * @return the numeric value represented by this object after conversion 75 * to type {@code int}. 76 */ 77 public abstract int intValue(); 78 79 /** 80 * Returns the value of the specified number as a {@code long}. 81 * 82 * @return the numeric value represented by this object after conversion 83 * to type {@code long}. 84 */ 85 public abstract long longValue(); 86 87 /** 88 * Returns the value of the specified number as a {@code float}. 89 * 90 * @return the numeric value represented by this object after conversion 91 * to type {@code float}. 92 */ 93 public abstract float floatValue(); 94 95 /** 96 * Returns the value of the specified number as a {@code double}. 97 * 98 * @return the numeric value represented by this object after conversion 99 * to type {@code double}. 100 */ 101 public abstract double doubleValue(); 102 103 /** 104 * Returns the value of the specified number as a {@code byte}. 105 * 106 * @implSpec 107 * The default implementation returns the result of {@link #intValue} cast 108 * to a {@code byte}. 109 * 110 * @return the numeric value represented by this object after conversion 111 * to type {@code byte}. 112 * @since 1.1 113 */ 114 public byte byteValue() { 115 return (byte)intValue(); 116 } 117 118 /** 119 * Returns the value of the specified number as a {@code short}. 120 * 121 * @implSpec 122 * The default implementation returns the result of {@link #intValue} cast 123 * to a {@code short}. 124 * 125 * @return the numeric value represented by this object after conversion 126 * to type {@code short}. 127 * @since 1.1 128 */ 129 public short shortValue() { 130 return (short)intValue(); 131 } 132 133 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 134 @java.io.Serial 135 private static final long serialVersionUID = -8742448824652078965L; 136 }