1,Integer.valueOf()和Integer.parseInt()的作用:
- Integer.valueOf()和Integer.valueOf()这两个方法都是Integer的静态方法,都可以传入一个只包含整数的字符串类型,将其转换为整数。
String str = "123";
Integer.valueOf(str);
Integer.parseInt(str);
2,Integer.valueOf()和Integer.parseInt()的不同:
- Integer.valueOf()和Integer.valueOf()两个方法的返回值类型不一样
Integer.valueOf()返回一个Integer类型的数据,是int的包装类
Integer.parseInt()返回一个int类型的数据。
如果使用int类型数据接受返回值,两者没有什么不同,但是如果使用Integer类型数据接受返回值,Integer.valueOf()会报警告。
3,Integer.valueOf().intValue()和Integer.parseInt()是相同的:
- Integer.valueOf().intValue()和Integer.parseInt()都返回int类型数据,他们两个的结果是完全相同的。
4,自动装箱和自动拆箱
自动装箱
int a = 10;
Integer b = a;
Integer c = 10;
自动拆箱
String str = "123";
Integer a = new Integer(str);
int b = a;