数据类型转换[关闭]

问题描述 投票:-8回答:1
 Float f;
 Long l;
 Char ch;
 Byte b;
 l=f;
 ch=b;

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

java
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.