包装类认识与总结

包装类

1、基本数据类型对应的包装类

byte Byte

short Short

int Integer

long Long

float Float

double Double

char Charter

boolean Boolean

2、数字转化为字符串

1、基本数据类型数据的值+"" 最简单的方式(工作中常用)
2、使用包装类中的静态方法
    static String toString(int i);返回一个表示指定整数的String对象【字符串对象】
3、使用String类中静态方法
    static String valueOf(int i);返回int参数的字符串表示形式

3、字符串转化为数字

除了Character类之外,其他所有包装类都具有parseXxx静态方法可以将字符串参数转换为对应的基本类型:
    public static byte parseByte(String s):将字符串参数转换为对应的byte基本类型。
    public static short parseShort(String s):将字符串参数转换为对应的short基本类型。
    public static int parseInt(String s):将字符串参数转换为对应的int基本类型。
    public static long parseLong(String s):将字符串参数转换为对应的long基本类型。
    public static float parseFloat(String s):将字符串参数转换为对应的float基本类型。
    public static double parseDouble(String s):将字符串参数转换为对应的double基本类型。
    public static boolean parseBoolean(String s):将字符串参数转换为对应的boolean基本类型。

4、自动拆装箱

装箱:把基本数据类型的数据,包装到包装类中(基本数据类型-->包装类)
    构造方法:
        Integer(int value);构造一个新分配的 Integer 对象,它表示指定的 int 值
        Integer(String s);构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
            所传递的字符串参数,必须是基本数据类型的字符串,否则会抛出异常:如“100”正确,“a”则抛异常

    静态方法:
        static Integer valueOf(int i);返回一个表示指定的 int 值的 Integer 实例。
        static Integer valueOf(String s);返回保存指定的 String 的值的 Integer 对象。

拆箱:在包装类中取出基本数据类型的数据(包装类-->基本数据类型)
    成员方法:
         int intValue() 以 int 类型返回该 Integer 的值。
         long longValue() 以 long 类型返回该 Integer 的值。

 

 

 

猜你喜欢

转载自www.cnblogs.com/elvin-j-x/p/12708693.html
今日推荐