再美不及姑娘你
又见西风上碧树

一、概述:

将基本数据类型封装成对象

优点:

可以在对象中定义更多的功能方法操作该数据

常见用法:

用于基本类型与字符串之间的转换

基本数据类型包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

二、Integer类

概述:包装一个对象中的原始类型int的值

方法名说明
public Integer(int value) 根据int值创建Integer对象(过时)
public Integer(String s) 根据String值创建Integer对象(过时)(字符串应是数字组成的)
public static Integer valueOf(int i) 返回表示指定的inter值的Integer实例
public static Integer valueOf(String s) 返回一个保存指定值的Integer对象String(NumberFormatException不是数字字符串报的错误)

三、int与String之间的相互转换

  1. int 转换成 String

    public static String Valueof(int i):返回int 参数的字符串形式,该方法是String类中的方法

         //int 转换成字符串
          int num=100;
          //方式一
          String s=""+num;//进行拼接将int类型转换成String
          System.out.println(s);
          //方式2:public static String valueOf(int i)
          String s1=String.valueOf(num);
          System.out.println(s1);

    2.String 转换成int

    public static int praseInt(String s):将字符串解析为int类型,该方法是integer类中的方法

    //String 转换成int
          String s2="100";
          //方式一: String-integer-int
          Integer i=Integer.valueOf(s2);
          // public int intValue(),该方法不是静态,通过对象来调用
          int x=i.intValue();
          System.out.println(x);
          //方法二:public static int praseInt(String s)
          int x1=Integer.parseInt(s2);
          System.out.println(x1);
  2.  

posted on 2022-03-22 13:27  再美不及姑娘你  阅读(72)  评论(0)    收藏  举报