首页 > 代码库 > 基本类型补充练习代码

基本类型补充练习代码

 public class TestConvert2{
public static void main(String []args){
int i=1;
int j=2;
float f1=0.1f;
float f2=123;
long l1=12345678,l2=8888888888L;//编译器看到整数时自动把其当成int型,所以L2后面必须加L使其为long类型
double d1=2e20,d2=124;
byte b1=1,b2=2,b3=127;//129会溢出,换位123
j=j+10;
i=i/10;
i=(int)(i*0.1);//这里需要强制类型转换 i*0.1结果为double
char c1=‘a‘,c2=125;
byte b=(byte)(b1-b2);//需要强制转换,b1-b2结果为int型
char c=(char)(c1+c2-1);//同样需强制转换,c1+c2-1结果为int型,int型赋值给char型需强制转换
float f3=f1+f2;
float f4=(float)(f1+f2*0.1);//需要强制转换,f1+f2*0.1结果为double型
double d=d1*i+j;
float f=(float)(d1*5+d2);
 
}
}

基本类型补充练习代码