Java之基本数据类型和String类型的转换
原创
©著作权归作者所有:来自51CTO博客作者兮动人的原创作品,请联系作者获取转载授权,否则将追究法律责任
1. 基本数据类型和String类型的转换
1.1 介绍和使用
int n1 = 100;
float f1 = 1.1F;
double d1 = 4.5;
boolean b1 = true;
String s1 = n1 + "";
String s2 = f1 + "";
String s3 = d1 + "";
String s4 = b1 + "";
System.out.println(s1 + " " + s2 + " " + s3 + " " + s4);
String s5 = "123";
int num1 = Integer.parseInt(s5);
double num2 = Double.parseDouble(s5);
float num3 = Float.parseFloat(s5);
long num4 = Long.parseLong(s5);
byte num5 = Byte.parseByte(s5);
boolean b = Boolean.parseBoolean("true");
short num6 = Short.parseShort(s5);
System.out.println("===================");
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
System.out.println(num4);
System.out.println(num5);
System.out.println(num6);
System.out.println(b);
System.out.println(s5.charAt(0));
1.2 注意事项
- 案例:StringToBasicDetail.java
String str = "hello";
int n1 = Integer.parseInt(str);
System.out.println(n1);