-2
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

Prajun Lungeli
  • 117
  • 1
  • 12

4 Answers4

1

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: 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.

varijkapil13
  • 390
  • 4
  • 16
  • I got that point, but just want to know, does the JVM implicitly perform the data type conversion in the first case by omitting 0.5? Because we know that converting double to int takes explicit action. – Prajun Lungeli Jul 22 '20 at 07:12
  • No, 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 id stored in `double`. So there is no type conversion happening, but the division takes place based on the input types. I am updating my answer to show this info. – varijkapil13 Jul 22 '20 at 08:19
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.

René
  • 358
  • 1
  • 10
  • 1
    "When dividing an int with an int it cuts off everything after the decimal point" - i suppose this is somewhat imprecise. The division is done strictly in the integer domain, so there is no "decimal point cut off". Its just an integer division result (3) and some remainder (1). – Gyro Gearless Jul 22 '20 at 07:08
  • @GyroGearless youre right of course but for better understanding if you would devide 7 by 2 the result would be 3.5 but since youre doing integer division for human understanding you take the result and cut off everything after the decimal point ( floor ) like casting – René Jul 22 '20 at 07:16
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 ..

Ozwiya
  • 46
  • 2
0

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
Ashish Mishra
  • 704
  • 1
  • 6
  • 20