# By dividing 7/2 gives 3.5, but why an incompatible error isn't shown in the first case compare to the second case and how .5 is omitted?

go评论16阅读模式

By dividing 7/2 gives 3.5, but why an incompatible error isn't shown in the first case compare to the second case and how .5 is omitted?

# 问题

``````//第一个案例
public class YourClassNameHere {
public static void main(String[] args) {
int num=7;
int div=num/2;
System.out.println(div);
}
}

//输出: 3

//第二个案例
public class YourClassNameHere {
public static void main(String[] args) {
double num=3.5;
int div=(int)num;
System.out.println(div);
}
}

//输出: 不兼容的类型: 从double到int可能有损失的转换
``````

``````public class YourClassNameHere { //first case
public static void main(String[] args) {
int num=7;
int div=num/2;
System.out.println(div);
}
}
``````

//output: 3

``````public class YourClassNameHere {//second case
public static void main(String[] args) {
double num=3.5;
int div=num;
System.out.println(div);
}
}
``````

//output: incompatible types: possible lossy conversion from double to int

# 答案1

`int` 不适合存储小数。它只能存储整数，例如 `1, 2342, -2323, 32` 等。

`double` 则适合存储包含小数的大数，例如 `12.12, 12.0008, 0.6787` 等等。

`int` is not meant to store fractions. It stores only whole numbers, such as `1, 2342, -2323,32` etc.

Where as double on the other hand is meant to store large number that contains fractions, eg `12.12, 12.0008, 0.6787` and so on.

So `12.005` is a valid `double` but when you "convert" this to an `int` it will lose the `.005` and only be `12`.

Similarly when you divide two numbers, and result should contains fractions, you should either store it in `float` or a `double`.

Edit

In the first case you are dividing and `int` with an `int`. So the result you get will be an `int`. If you notice the output is 3 when the result is stored in an `int`, and 3.0 if the result is stored in `double`. So there is no type conversion happening, but the division takes place based on the input types.

# 答案2

When dividing an int with an int it cuts off everything after the decimal point, so the result stays an int, and since `num` is an int and `2` is an int the result is 3 which can be stored into `div`.

In the second scenario you are trying to store a double into an int, which is not possible. Try out following `div = num / 2.0`. By dividing by `2.0`, which is a double, the result should also be a double and then youll get the same error.

# 答案3

in your first case you dividing two int it's mean any answer will be without decimal point so any answer will omit the decimal point .. in the second case you can't set double value in int variable because there is a decimal point in double and not in int .. when you want to have the answer with decimal point just use double no need for int u have both option for your need ..

# 答案4

``````int brothers = 2, backlogs = 3, lockdown = 3;
int birthToDeathRatio = 0.72; 错误
//它会抛出错误：不兼容的类型：可能会丢失从double到int的转换

float birthToDeathRatio = 0.72; //(6到7位小数精度)
double birthToDeathRatio = 0.72; //(15到16位小数精度)

int div = (int)(7/2); 或 int div = (int)(3.5);
//输出 3
``````

You are using integer type to store the double type numbers.
Integers can only store whole numbers.

``````int brothers = 2, backlogs = 3, lockdown = 3;
int birthToDeathRatio = 0.72; wrong
//It will throw error incompatible types: possible lossy conversion from double to int
``````

so to store the fractions you can use

``````float birthToDeathRatio = 0.72;//(6 to 7 decimal precision)
double birthToDeathRatio = 0.72;//(15 to 16 decimal precision)
``````

or if you are obliged to store the result in integer you can explicity cast it

``````int div = (int)(7/2); or int div = (int)(3.5);
//output 3
``````

• 本文由 发表于 2020年7月22日 14:51:02
• 转载请务必保留本文链接：https://go.coder-hub.com/63028479.html
• compiler-errors
• division
• java
• java-8
• type-conversion

go 13

go 14

go 21

go 17