Number抽象类源码注释解析
1 package java.lang; 2 3 /** 4 * The abstract class {@code Number} is the superclass of platform 5 * classes representing numeric values that are convertible to the 6 * primitive types {@code byte}, {@code double}, {@code float}, {@code 7 * int}, {@code long}, and {@code short}. 8 * 9 * The specific semantics of the conversion from the numeric value of 10 * a particular {@code Number} implementation to a given primitive 11 * type is defined by the {@code Number} implementation in question. 12 * 13 * For platform classes, the conversion is often analogous to a 14 * narrowing primitive conversion or a widening primitive conversion 15 * as defining in <cite>The Java™ Language Specification</cite> 16 * for converting between primitive types. Therefore, conversions may 17 * lose information about the overall magnitude of a numeric value, may 18 * lose precision, and may even return a result of a different sign 19 * than the input. 20 * 21 * See the documentation of a given {@code Number} implementation for 22 * conversion details. 23 * 24 * @author Lee Boynton 25 * @author Arthur van Hoff 26 * @jls 5.1.2 Widening Primitive Conversions 27 * @jls 5.1.3 Narrowing Primitive Conversions 28 * @since JDK1.0 29 */ 30 public abstract class Number implements java.io.Serializable { 31 /** 32 * Returns the value of the specified number as an {@code int}, 33 * which may involve rounding or truncation. 34 * 35 * @return the numeric value represented by this object after conversion 36 * to type {@code int}. 37 */ 38 public abstract int intValue(); 39 40 /** 41 * Returns the value of the specified number as a {@code long}, 42 * which may involve rounding or truncation. 43 * 44 * @return the numeric value represented by this object after conversion 45 * to type {@code long}. 46 */ 47 public abstract long longValue(); 48 49 /** 50 * Returns the value of the specified number as a {@code float}, 51 * which may involve rounding. 52 * 53 * @return the numeric value represented by this object after conversion 54 * to type {@code float}. 55 */ 56 public abstract float floatValue(); 57 58 /** 59 * Returns the value of the specified number as a {@code double}, 60 * which may involve rounding. 61 * 62 * @return the numeric value represented by this object after conversion 63 * to type {@code double}. 64 */ 65 public abstract double doubleValue(); 66 67 /** 68 * Returns the value of the specified number as a {@code byte}, 69 * which may involve rounding or truncation. 70 * 71 * <p>This implementation returns the result of {@link #intValue} cast 72 * to a {@code byte}. 73 * 74 * @return the numeric value represented by this object after conversion 75 * to type {@code byte}. 76 * @since JDK1.1 77 */ 78 public byte byteValue() { 79 return (byte)intValue(); 80 } 81 82 /** 83 * Returns the value of the specified number as a {@code short}, 84 * which may involve rounding or truncation. 85 * 86 * <p>This implementation returns the result of {@link #intValue} cast 87 * to a {@code short}. 88 * 89 * @return the numeric value represented by this object after conversion 90 * to type {@code short}. 91 * @since JDK1.1 92 */ 93 public short shortValue() { 94 return (short)intValue(); 95 } 96 97 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 98 private static final long serialVersionUID = -8742448824652078965L; 99 }
这个抽象类Number是可以转换为基本数据类型(byte,double,float,int,long,short)的展示数值的类的父类
从特定Number实现类转换到一个给定的基础类型的方法是由Number类定义的(..Value())
在java基本数据类型转换语言规范定义中,对类来说,转换通常类似于一个窄化或者扩大。因此,转换可能会丢失过大数值的信息,可能会丢失精度,甚至可能返回与输入不同的符号的结果。
将指定数字的值作为int/long/float/doube/byte/short返回,这可能涉及舍入/截断。