Search code examples
javatypesoperator-precedence

Are mathematical operations different for different data types?


Why is it that in the first line sevenTwelfths will evaluate to the expected answer (0.5833), but threeTwentySixths will evaluate to zero? I assumed that since the data type is a double, the operation of dividing 3 by 26 would be a decimal, but it appears as if it does the operation as an integer operation and then coverts that answer to a double and stores it in threeTwentySixths.

    double sevenTwelfths = ((double) 7 / 12);
    double threeTwentySixths = 3 / 26;

Solution

  • double sevenTwelfths = ((double) 7 / 12);

    In First line this called typecasting meaning you are converting your answer in double for example

    int sevenTwelfths=((int)7/12)); this mean after dividing 7/12 whatever is the anser convert into int or Type cast into int