/**
*1)、学习数据类型之间的转换(自动转换+强制转换)方法;
*2)、自动转换:精度低的数据类型-->精度高的数据类型;强制转换:精度高的数据类型-->精度低的数据类型;
*3)、boolean类型不能进行类型转换;
*4)、本例仅举出了少部分的常规使用和比较特殊的例子,其它常规使用请自行“举一反三”;
*/
class tt1_4
{
public static void main(String[] args)
{
float a = 2.84f;
System.out.println((int)a); //注意:浮点型向int型转换时的舍弃方式;
int b = 300;
System.out.println((byte)b); //注意:byte型数据最大值为:127;
//21 = 2;22 = 4;23 = 8;25 = 32;27=128;28=256;28 + 25 = 288;
//28 + 25 + 23 + 22 = 300;
//int b = 300 ==> 000,000,000,000,000,000,000,001,001,011,00;
//(byte)b=44==> 001,011,00;
//25 + 23 + 22 = 44;
double c = 100.4;
System.out.println(b + c); //注意:这里是自动转换;
char d = 'a';
char e = 97; //注意:这里的"97"实际是个int型数据;
String str_1 = "97"; //注意:把str_1换成"a"试试看?哈哈! System.out.println(d);
System.out.println(e);
System.out.println(str_1);
System.out.println(d + e); //注意:这里是自动转换;
System.out.println(Integer.valueOf(str_1));
System.out.println(Integer.parseInt(str_1));
System.out.println(Integer.valueOf(str_1).intValue());
int f = 65;
int g = 32;
System.out.println((char)(f + g)); //注意:这里是数字向字符的强制转换;
}
}
【学习心得】-数值转换问题小思-0
最新推荐文章于 2022-02-05 15:17:08 发布