public static int parseInt(String s) {
把字符串转换成基本数据类型
}
包装类
数据类型:基本数据类型+引用型(对象)
基本类型 ======== 封装类
boolean Boolean
byte Byte
short Short
int Integer
long Long
float Flaot
double Double
(以Integer为例讲解)
2.1 构造函数
public Integer(int); 整型100 封装成Integer对象
public Integer(String); "100"
public class Test {
public static void main(String[] args) {
Integer i1 = new Integer(100);
Integer i2 = new Integer("100")
}
}
2.2静态成员变量
最大整数:Integer.MAX_VALUE; public static finalint MAX_VALUE = 0x80000000;
最小整数: Integer.MIN_VALUE; public static final int MAX_VALUE =0x7fffffff;
2.3成员方法
2.3.1 parseInt() <=======> String.valueOf()
public static int parseInt(String s) {
//把字符串转换成基本数据类型
}
public class Test {
public static void main(String[] args) {
String qqStr = "110";
int qq = Integer.parseInt(qqStr);
System.out.println(qq+1); //111
}
}
2.3.2 intValue()
public int intValue() {
//把包装类转化成基本数据类型
}
public class Test {
public static void main(String[] args) {
Integer inte = new Integer(100); // 基本整数100 转化成 对应包装类对象
int i = inte.intValue(); //包装类对象还原成基本整数100
System.out.println(i+1); //101
Integer in = 200; // 自动拆箱
int i = in; // 也不出错,自动拆箱
}
}
public class Test {
public static void main(String[] args) {
Integer i = new Integer(100);
Double d = new Double("123.456");
int j = i.intValue() + d.intValue();
System.out.println(j); // 223
int i = Integer.parseInt("lss");
// java.lang.NumberFormatException (数字格式异常)
}
}