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?

huangapple go评论47阅读模式
英文:

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

得分: 1

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

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

所以 12.005 是一个有效的 double,但当你将其“转换”为 int 时,会丢失 .005,只剩下 12

类似地,当你除法运算两个数字,结果应包含小数时,你应该将其存储在 floatdouble 中。

详见这个问题以获取更多信息:https://stackoverflow.com/questions/23555019/difference-between-int-and-double

编辑

在第一个情况下,你正在对一个 int 进行除法运算,结果将是一个 int。如果你注意到,当结果存储在一个 int 中时,输出是3,而当结果存储在 double 中时,输出是3.0。因此,没有类型转换发生,但是根据输入类型进行了除法运算。

英文:

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.

See this question for more info: https://stackoverflow.com/questions/23555019/difference-between-int-and-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

得分: 0

当将一个整数除以一个整数时,它会截断小数点后的所有内容,因此结果仍然是一个整数,而由于num是一个整数,2也是一个整数,所以结果是3,可以存储在div中。

在第二种情况下,您试图将一个双精度数存储到一个整数中,这是不可能的。尝试以下代码div = num / 2.0。通过除以2.0,这是一个双精度数,结果也应该是一个双精度数,然后您将获得相同的错误。

英文:

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

得分: 0

在第一种情况下,你正在将两个整数相除,这意味着任何答案都将没有小数点,因此任何答案都将省略小数点。在第二种情况下,你不能将双精度值设置为整数变量,因为双精度中有小数点而整数中没有。当你想要答案带有小数点时,只需使用双精度,不需要整数,你有两种选项来满足你的需求。

英文:

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

得分: 0

你正在使用整数类型来存储双精度类型的数字。
整数只能存储整数。

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

huangapple
  • 本文由 发表于 2020年7月22日 14:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63028479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定