数据类型转换

时间:2018-03-03 21:57:10

标签: java

 Float f;
 Long l;
 Char ch;
 Byte b;
 l=f;
 ch=b;

为什么这段代码会出错? (因为我知道Char和long的类型比每种数据类型都多)

1 个答案:

答案 0 :(得分:0)

你是什么意思?

你必须将f转换为long,将b转换为char并初始化它们。

  

l =(长)f;   ch =(char)b;

//----
float f = 3.14f;
long l ;
char ch;
byte b = 38;
l= (long) f;
ch= (char) b;   

检查示例:

int first = 3:
int second = 2;
double result = first / second;  // the result is 1 because first and second are integers

first = 3;
second = 2;
double result1 = (double)first / second;  // result is: 1.5

double result2 = first / (double)second;  // result is: 1.5

double result3 = (double)(first / second);  // result is: 1