java中基本类型和字符串之间的转换

来源:互联网 发布:肩颈按摩仪 知乎 编辑:程序博客网 时间:2024/06/05 02:59

1、基本类型转换为字符串有三种方法

      a、使用包装类的toString()方法

      b、使用String类的valueOf()方法

      c、用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串


  eg:

          int c = 10;

          String   str1=Integer.toString(c);//方法1

          String   str2=String.valueOf(c);//方法2

          String   str3=c+“”;  //方法3

2、将字符串转换成基本类型有两种方法

       a、调用包装类的parseXxx静态方法

       b、调用包装类的valueOf()方法转换为基本类型的包装类,会自动拆箱


   eg:

           String str=“8”;

           int a=Integer.parseInt(str);//方法1

           int b=Integer.valueOf(str);//方法2


原创粉丝点击