//整数与浮点数的转换
//精度高的向精度低的需要强转
int i = (int)1.9;//1.9是浮点数前面加上()强转为int,那么会丢失小数位上的数字只留下整数
System.out.println(i);//1
//精度低向精度高会自动转换不需要强转
int j = 10;
float k = j;
System.out.println(k);//10.0
double m = 12.1324548;
float n = 12.1324548F;//java的浮点型常量(具体值)默认为double型,
//声明flot需要在后加上“F”或“f”。
System.out.println(m);//12.1324548
System.out.println(n);//12.132455
//整数与字符串的互转
char m2 = 100;
System.out.println(m2);//char类是可以保存int的常量值,但是不能保存int的变量值,需要强转
int c1= 100;
char c2= (char)c1;
System.out.println(c2);//输出100对应的字符“d”
//基本数据类型->String
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->对应的基本数据类型
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);//123
System.out.println(num2);//123.0
System.out.println(num3);//123.0
System.out.println(num4);//123
System.out.println(num5);//123
System.out.println(num6);//123
System.out.println(b);//true
//把字符串转成字符 char -> 含义是指 把字符串的第一个字符得到
System.out.println(s5.charAt(0)); //1
整数与浮点数的转换,整数与字符串的转换(java)。
于 2022-12-15 17:07:52 首次发布