在C++或Java中,类型转换和类型转换有什么不同?
发布于 2010-10-29 14:43:38
类型转换只是拿起一支笔,在变量上写上"this is a int“,转换实际上是将内容转换为所需的类型,因此值保持有意义。
发布于 2010-10-29 14:48:40
类型转换:
double value = 3; // implicit conversion to double value 3.0
int nValue = 3.14156; // implicit conversion to integer value 3
强制转换是程序员进行显式类型转换的请求。
int nValue = 10;
double dvalue = double(nValue); // explicit type casting
发布于 2010-10-29 14:44:07
根据维基百科article
在C语言系列中,单词cast通常指显式类型转换(而不是隐式转换),无论这是位模式的重新解释还是实际转换。
下面是一个C++示例:
double d = 42.0;
int i = d; // Here you have an implicit conversion from double to int
int j = static_cast<int>(d); // Here you have a cast (explicit conversion).
下面是一个Java示例(注意,与C++不同的是,在Java中您不能隐式地将double转换为int):
int i = 42;
double d = i; // Here you have an implicit conversion from int to double
int j = (int)d; // Here you have a cast (explicit conversion).
https://stackoverflow.com/questions/4049580
复制相似问题