基本数据类型的对象包装类

(1)为了更方便的操作每个基本数据类型,java对其提供了很多的属性和方法供我们使用。
(2)用途:
**将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能操作该数据。
**常用的操作之一:用于基本数据类型与字符串之间的转换。
A:方便操作
B:用于和字符串进行相互转换
(3)基本数据类型和对象类型的对应
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
(4)构造方法

字段摘要:
static int MAX_VALUE 值为 2^31-1 的常量,它表示 int 类型能够表示的最大值        
static int MIN_VALUE  值为 -2^31 的常量,它表示 int 类型能够表示的最小值
static Class<Integer> TYPE 表示基本类型int的Class 实例
         
Integer(int value) 构造一个新分配的Integer对象,它表示指定的int值。
Inreger(String s) 注意:s必须是纯数字的字符串。否则会有异常NumberFormatException
                       
(5)几个常用的方法
Integer.toBinaryString();
以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
Integer.toOctalString();
以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。
Integer.toHexString();
以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
static int Integer.parseInt(String s) 将字符串参数作为有符号的十进制整数进行解析,
字符串必须是int型范围内的数字字符串
static int Integer.parseInt(String s,int basic)
使用第二个参数指定的基数,将字符串参数解析为有符号的整数.
字符串必须是int型范围内的数字字符串
short shortValue() 以short类型返回该Integer的值。         
int intValue() 以int类型返回该Integer的值。 
static Integer valueOf(int num) 返回一个表示指定的 int 值的 Integer 实例。
static Integer valueOf(String s) 返回保存指定的String的值的Integer对象。          
                static Integer valueOf(String s, int radix)
返回一个Integer对象,该对象中保存了用第二个参数提供的基数进行
解析时从指定的String中提取的值。

(6)类型转换
int -- Integer
int num = 20;
A:Integer i = new Integer(num);
B:Integer i = Integer.valueOf(num);
Integer -- int
Integer i = new Integer(20);
A:int num = i.intValue();

int -- String
int num = 20;
A:String s = String.valueOf(num);
B:String s = ""+num;
C:String s = Integer.toString(num);
String -- int
String s = "20";
A:int num = Integer.parseInt(s);
B:Integer i = new Integer(s);或者Integer i = Integer.valueOf(s);
  int num = i.intValue();

猜你喜欢

转载自YANGYUANIT.iteye.com/blog/2383728