If a smaller data type is assigned to a bigger data type, there'll be no error. But the assignment of bigger to smaller gives error. In this case, you need to make compatible these data types with each other using type conversion ('x = (Type) y
'). Converting a double
to int
is an example of assigning a bigger data type (double
) to smaller (int
). When we perform this operation, the double
variable lost its precision and its "integer part" is assigned to the int
variable.
double x = 3, y = 2;
x /= y;
int integerPart = (int) x;
System.out.println(integerPart); // Prints 1
From small to big, the numeric data types are as follows btw:
byte < short < int < long < float < double
Edit: After your last edit I realized what you actually ask. Your first expression was wrong. You don't want to find integer part of the double
result of division, you want its floor. Just use java.lang.Math.floor
:
double[] x = {-10, -7, 1, 3, 7.1, 9.5};
double[] y = {-10, -7, -1.7, 0.5, 7.1, 9.5};
for (int i = 0; i < y.length; i++) {
for (int j = 0; j < x.length; j++)
System.out.print(Math.floor(x[j] / y[i]) + " ");
System.out.println();
}