Java的数据类型分为三大类,即布尔型、字符型和数值型,而其中数值型又分为整型和浮点型;
相对于数据类型,Java的变量类型为布尔型boolean;字符型char;整型byte、short、int、long;浮点型float、double.
对于这些变量类型之间的相互转换在我们编程中经常要用到,
这些类型由低级到高级分别为(byte,short,char)——int——long——float——double,低级变量可以直接转换为高级变量,
byte b;
int i=b;
高级变量转换为低级变量时,可以使用强制类型转换。
int i;
byte b=(byte)i;
以下是常见的String类型与各类型之间的相互转换
String 转成 Date类型
String sdate="2012-11-12";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sdate);
Date类型转换成String类型
Date date = new Date();
SimpleDateFormate sdf = new SimpleDateFormat("yyyy-MM-dd");
String sdate = sdf.format(date);
Integer、Double、Long等数据包装类型与String 类型的相互转换
Integer类型转成String
Integer a;
String s = a.toString();
String 类型转成Integer
String s;
Integer a = Integer.valueOf(s);