由于这几个东西转来转去老是搞混了,所以我特意做了一个这三者之间互转的总结,代码如下:
public class Main {
public static void main(String[] args) {
String str = "12345";
Integer integer = 123456;
int i = 123456;
//int转String
String myStr1 = String.valueOf(i);
//int转String
String myStr2 = i + "";
//int转Integer
Integer myInteger1 = i;//自动装箱,就不用后面那个了//Integer.valueOf(i);
//String转Integer
Integer myInteger2 = Integer.valueOf(str);
//String转int
int myInt1 = Integer.parseInt(str);
//Integer转int
int myInt2 = myInteger2;//自动拆箱,就不用后面那个了//myInteger2.intValue();
//Integer转String
String myStr3 = String.valueOf(integer);
//Integer转String
String myStr4 = integer + "";
//Integer转String
String myStr5 = myInteger1.toString();
//int转String
String myStr6= Integer.toString(i);
}
}
本文通过示例代码展示了Java中int、String和Integer类型之间的转换过程,包括自动装箱和拆箱机制。
343

被折叠的 条评论
为什么被折叠?



