203 int和string的相互转换
String.valueOf(valueName)
Integer.parseInt(valueNames)
基本类型包装类最常见的操作:用于基本类型和字符串之间的相互转换
其他转为String
方式1,,空字符串和number拼接成为s1,则number成为s1,效果达到,但显得不专业
方式2,String.valueOf(type value),括号里是参数,可选8种,需要哪种选哪种。方法执行结果是,参数转换为String类型的数据
其他转为int
方式1,String转为integer,转为int
//方式2,从手册找到的参数为string,返回值为int的方法
--------------------------------------------------------------
package it03e203;
public class IntegerDemo {
public static void main(String[] args) {
System.out.println("---int to String---");
int number = 203;
//方式1,空字符串和number拼接成为s1,则number成为s1,效果达到,但显得不专业
String s1 = ""+number;
System.out.println("方式1,空字符串和number拼接成为s1:"+s1);
//方式2,String.valueOf(int value),返回以int为参数的字符串表现形式
String s2 = String.valueOf(number);
System.out.println("方式2,String.valueOf(int value):"+s2);
System.out.println("---String to int---");
String s = "203";
//方式1,String转为integer,转为int
Integer i = Integer.valueOf(s);
int x = i.intValue();
System.out.println("方式1,String-integer-int:"+x);
//方式2,从手册找到的参数为string,返回值为int的方法
int y = Integer.parseInt(s);
System.out.println("方式2,从手册找到的参数为string,返回值为int的方法:"+y);
}
}
--------------------------------------------------------------
output:
---int to String---
方式1,空字符串和number拼接成为s1:203
方式2,String.valueOf(int value):203
---String to int---
方式1,String-integer-int:203
方式2,从手册找到的参数为string,返回值为int的方法:203