JAVA中把数字转换为字符串,字符串转换为数字

来源:互联网 发布:阿里云os应用下载 编辑:程序博客网 时间:2024/05/19 20:44

  • 数字转字符串

方法1: 使用String类的静态方法valueOf 
方法2: 先把基本类型装箱为对象,然后调用对象的toString

public class TestNumber {      public static void main(String[] args) {        int i = 5;                 //方法1        String str = String.valueOf(i);                 //方法2        Integer it = i;        String str2 = it.toString();             }}

  • 字符串转数字
调用Integer的静态方法parseInt
public class TestNumber {      public static void main(String[] args) {         String str = "999";                 int i= Integer.parseInt(str);                 System.out.println(i);             }}


原创粉丝点击