14.1.3 Byte, Short, Integer 和 Long
Byte,Short,Integer,和Long类分别是字节型(byte),短整型(short),整型(int)和长整型(long)整数类型的包装器。它们的构造函数如下:
Byte(byte num)
Byte(String str) 引发一个NumberFormatException异常
Short(short num)
Short(String str) 引发一个NumberFormatException异常
Integer(int num)
Integer(String str) 引发一个NumberFormatException异常
Long(long num)
Long(String str) 引发一个NumberFormatException异常
正如你能看到的,这些对象可由数值或含有有效整数值的字符串创建。由这些类定义的方法列在表14-3到表14-6中。正如你能看到的,它们定义方法以便从字符串解析整数和将字符串转换为整数。为方便起见,这些方法提供的变量可以用来指定radix,也称为基数。通常二进制(binary)的基数是2,八进制(octal)的基数是8,十进制(decimal)的基数是10,而十六进制(hexadecimal)的基数为16。
表14-3 由Byte 定义的方法
方法 描述
byte byteValue() 返回调用对象值(字节型)
int compareTo(Byte b) 将调用对象的数值与b的数值进行比较。如果这两个数值相等,则返回0。如果调用对象的数值小于b的数值,则返回负值。如果调用对象的数值大于b的数值,则返回正值(在Java2中新增加的)
int compareTo(Object obj) 如果obj属于类Byte,其操作与compareTo(Byte)相同。否则,引发一个ClassCastException异常(在Java 2中新增加的)
static Byte decode(String str)
throws NumberFormatException
返回一个包含了由str中的字符串指定的值的Byte对象
double doubleValue() 返回调用对象值(双精度度型)
boolean equals(Object ByteObj) 如果Byte调用对象与ByteObj相等,则返回true。否则,返回false
float floatValue() 返回调用对象值(浮点型)
int hashCode() 返回调用对象的散列码
int intValue() 返回调用对象值(整型)
long longValue() 返回调用对象值(长整型)
static byte parseByte(String str)
throws NumberFormatException
以10为基数,返回包含在由str指定的字符串中的数字的等价
字节(byte)形式
static byte parseByte(String str,int radix)
throws NumberFormatException
以指定的基数(radix)为底,返回包含在由str指定的字符串
中的数字的等价字节
short shortValue() 返回调用对象值(短整型)
String toString() 返回一个包含了调用对象的等价十进制形式的字符串
static String toString(byte num) 返回一个包含了num的等价十进制形式的字符串
static Byte valueOf(String str)
throws NumberFormatException
返回一个包含了由str中的字符串指定的值的Byte对象
static Byte valueOf(String str,int radix)
throws NumberFormatException
以指定的基数(radix)为底,返回一个包含了由str中的字符串指定的值的Byte对象
表14-4 由Short 定义的方法
方法 描述
byte byteValue() 返回调用对象值(字节型)
int compareTo(Short s) 将调用对象的数值和s的数值进行比较。如果这两个值相等,则返回0。如果调用对象的数值小于s的数值,则返回负值如果调用对象的数值大于s的数值,则返回正值(在Java 2中新增加的)
int compareTo(Object obj) 如果obj属于类Short,其操作与compareTo(Short)相同。否则,引发一个ClassCastException异常(在Java 2中新增加的)
static Short decode(String str)
throws NumberFormatException
返回一个包含了由str中的字符串指定值的Short对象
double doubleValue() 返回调用对象值(双精度型)
boolean equals(Object ShortObj) 如果整型(Interger)调用对象与ShortObj相等,则返回true。否则,返回false
float floatValue() 返回调用对象值(浮点值)
int hashCode() 返回调用对象的散列码
int intValue() 返回调用对象值(整型)
long longValue() 返回调用对象值(长整型)
static short parseShort(String str) throws
NumberFormatException
以10为基数,返回包含在由str指定的字符串中的数字的等价短整型(Short)数
static short parseShort (String str,int radix)
throws NumberFormatException
以指定的基数(radix)为底,返回包含在由str指定的字符串中的数字的等价短整型(Short)数
short shortValue() 返回调用对象值(短整型)
String toString() 返回一个包含了调用对象的等价十进制形式的字符串
static String to String(short num) 返回一个包含了num的等价十进制形式的字符串
static Shortvalue Of(Stringstr)throws
NumberFormatException
以10为基数,返回一个包含了由str中的字符串指定的值的Short对象
static Short valueOf(String str,int radix)
throws NumberFormatException