为什么Strange回答Java?

时间:2015-06-08 11:03:39

标签: java

我有以下代码

int num1 = 8;
int num2 = 5;

double ans = num1/num2;

System.out.println(ans); // result is 1.0

为什么我会在1.0 ans 1.6获得CREATE TEMP TABLE jsontesting AS SELECT id, jsondata::jsonb FROM ( VALUES (1, '["abra","value","mango", "apple", "sample"]'), (2, '["japan","china","india", "russia", "australia"]'), (3, '["must", "match"]'), (4, '["abra","value","true", "apple", "sample"]'), (5, '["abra","false","mango", "apple", "sample"]'), (6, '["string","value","mango", "apple", "sample"]'), (7, '["must", "watch"]') ) AS t(id,jsondata);

1 个答案:

答案 0 :(得分:5)

不,它不应该是1.6

这是如何编译的

  

int / int - >结果int。

即。 8/5 = 1

然后将int值赋值为double。

  

int - > double //是1.0

如果你使用它:

double ans =(double) num1/num2;

这会给1.6,因为num1将被视为双倍

double / int -> result double.