1、Integer类
Java对于基本数据类型int进行了封装,形成了Integer类。Integer是一个对象,所以应该从认识类的次序,即成员属性、构造方法和成员属性三方面来学习Integer类。
Integer常用成员属性有:
(1)MAX_VALUE表示最大整数值,其值为2的31次方-1;
(2)MIN_VALUE表示最小整数值,其值为2的负31次方。
Integer类构造方法有:
(1)public Integer(int value)
(2)public Integer(Strings)
因此建立的Integer类实例哟两种形式:
Integer A = new Integer(1);
Integer B = new Integer("1").
Integer的成员方法有20多个,常用方法有:
(1)比较两个Integer类实例大小的方法
public int compareTo(Integer anotherInteger)
如有Integer实例A和B,两种相比较:
int result = A.compareTo(B);
如A和B相等,返回result为0;
如A>B,返回result>0;
如A<B,返回result<0.
(2)把字符串转换为Integer类实例
public static int parseInt(String s) throws NumberFormatException;
public static int parseInt(String s,int radix) throws NumberFormatException
这方法会抛出异常,如果S有数字外的字符就会出现转换错误。第二个方法的参数radix表示进制,用它可进行进制的转换。注意方法是static的,不需使用new来建立实例。
(3)把一个Integer类实例转换为二进制形式字符串
public static String toBinaryString(int i)
(4)把一个Integer类实例转换为十进制字符串
public static String toString(int i)
(5)比较两Integer类实例是否相等。和compareTo()不同,只比较是否相等,不是大小,要着重注意两个的区别。还有就是别和==混淆了。
public boolean equals(Object obj)
(6)把简单类型int转换为对象类型Integer
public static Integer valueOf(int i)
(7)把对象类型Integer转换为简单类型int
public int intValue()

以上只是常见的Integer类成员方法,更多的要去参阅API.
评论
发表评论

您还没有登录,请登录后发表评论

Absolutely
搜索本博客
最近加入圈子
存档
最新评论