一、包装类
1.基本数据类型以及对应的包装类:
-
byte -> Byte
-
short -> Short
-
int -> Integer
-
long -> Long
-
float -> Float
-
double -> Double
-
char -> Character
-
boolean -> Boolean
注:这些类都在java.lang包,且在此包下的类在使用时不需要引包,直接使用即可。
2、包装类的意义:
- 让基本数据类型有面向对象的特征
- 封装了字符串转化成基本数据类型的方法(重点)
3.包装类常用方法:
-
Integer.parseInt() : 将 String 类型的数字转换成 int 类型
-
Long.paseLong():将字符串类型转换成 Long 类型
-
Double.parseDouble():将字符串类型转换成 double 类型
Demo代码示例:
public class Test {
public static void main(String[] args) {
String a = "12";
String b = "34";
System.out.println(a+b); // 1234
// 转型:parseInt()
// 字符串转成int的唯一方案
int c = Integer.parseInt(a);
int d = Integer.parseInt(b);
System.out.println(c+d); // 46
// 字符串转成double类型:paseLong()
String e = "1.25";
double f = Double.parseDouble(e);
System.out.println(f*6); // 7.5
// 转成long类型:paseLong()
long l = Long.parseLong("1234567");
System.out.println(l);
}
}