Integer 类对象包含一个 int 类型的字段。此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。
在 Integer 类内部包含一些和 int 类型操作有关的方法,表 1 列出了这些常用的方法。
方法 | 返回值 | 功能 |
---|---|---|
byteValue() | byte | 以 byte 类型返回该 Integer 的值 |
shortValue() | short | 以 short 类型返回该 Integer 的值 |
intValue() | int | 以 int 类型返回该 Integer 的值 |
toString() | String | 返回一个表示该 Integer 值的 String 对象 |
equals(Object obj) | boolean | 比较此对象与指定对象是否相等 |
compareTo(Integer anotherlnteger) |
int | 在数字上比较两个 Integer 对象,如相等,则返回 0; 如调用对象的数值小于 anotherlnteger 的数值,则返回负值; 如调用对象的数值大于 anotherlnteger 的数值,则返回正值 |
valueOf(String s) | Integer | 返回保存指定的 String 值的 Integer 对象 |
parseInt(String s) | int | 将数字字符串转换为 int 数值 |
注意:在实现将字符串转换为 int 类型数值的过程中,如果字符串中包含非数值类型的字符,则程序执行将出现异常。
编写一个程序,在程序中创建一个 String 类型变量,然后将它转换为二进制、八进制、十进制和十六进制输出。
40的二进制数是:101000
40的八进制数是:50
40的十进制数是:40
40的十六进制数是:28
Integer 类包含以下 4 个常量。
原文:https://www.cnblogs.com/drigon/p/15029626.html