1 Public classtestfloatordouble {2 3 Public Static voidMain (string[] args) {4Point NUM1 =NewPoint (84, 250);5Point num2 =NewPoint (21, 10);6 7 floatF1 = (num1.y-num2.y)/(num1.x-num2.x);8 floatF2 = (float) (NUM1.Y-NUM2.Y)/(num1.x-num2.x);9 Ten DoubleD1 = (num1.y-num2.y)/(num1.x-num2.x); One DoubleD2 = (Double) (NUM1.Y-NUM2.Y)/(num1.x-num2.x); A - System.out.println (F1); - System.out.println (F2); the - System.out.println (d1); - System.out.println (D2); - } + } - + classpoint{ A intx; at inty; - - PublicPoint (intXinty) { - This. x =x; - This. y =y; - } in}
Output:
3.03.80952383.03.8095238095238093
It's starting to get a little out of the way. Why the first one is 3.0 the second is 3.8 ....
This is actually F1. The resulting order is that (NUM1.Y-NUM2.Y)/(num1.x-num2.x) divide two integers, get an integral type, temporarily assume int result, and then convert result to float type, so there is a 3.0
The resulting order of F2 is that (float) (NUM1.Y-NUM2.Y) first converts the first number to float, and then divides it with float and int to get the float type
Java integer divide to get floating point type