java - Does forcing a type into a variable make the whole statement into the inserted type?
问题描述
I am at a dilemma as to whether the type double would change the variable of width
into a double or only length
:
double ratio = (double) length / width;
解决方案
Type of the variables length and width do not change after executing that line. The type you have given to those variables when you declare them, remains as it is. Assuming width and length is type int, I wrote the small program below to check the type.
public class CheckType{
void printType(int x) {
System.out.println(x + " is an int");
}
void printType(double x) {
System.out.println(x + " is an double");
}
public static void main(String []args){
int length=6; int width=2;
double ratio = (double) length / width;
CheckType x = new CheckType();
System.out.println(length);
System.out.println(width);
x.printType(ratio);
x.printType(length);
x.printType(width);
}
}
Output from the above program is:
6
2
3.0 is an double
6 is an int
2 is an int
推荐阅读
- javascript - 复选框中的Vuejs单击事件?
- spring - Spring Webflow:跳过视图状态/转换自动选择
- grafana-api - 无法使用 HTTP API 创建 Grafana 用户
- json - 存储 API 令牌的最佳实践
- python - 用python选择一行的第一个元素
- jquery - jQuery:基于 url 参数添加类
- docker - 无法通过主机上发布的端口访问 Docker Swarm 服务
- css - 如何在最小高度和最大高度 css 之间创建自动调整大小的空 div?
- javascript - 如何在数组变量中添加引导卡,以便将来可以添加更多而无需编写太多代码?
- pdf-generation - 使用 pdfmarks 重置表单域的 postscript 示例