-1

When dividing two 'int' variables and saving the result into a 'double' variable, anything to the right of the decimal point is just zero?

See the three examples below.

Thank you in advance, Mike

Example 1 public class MyClass {

public static void main(String[] args) {
    int x, y, answer;
    x = 70;
    y = 30;
    answer = x / y;
    System.out.print(answer);
}

}

Output = 2 (I understand the result, all variables defined as 'int')

Example 2 public class MyClass {

public static void main(String[] args) {
    int x, y;
    double answer;
    x = 70;
    y = 30;
    answer = x / y;
    System.out.print(answer);
}

}

Output = 2.0 (I don't understand the result, the variable answer is 'double' and I expected 2.3333333333333335)

Example 3 public class MyClass {

public static void main(String[] args) {
    double x, y, answer;
    x = 70;
    y = 30;
    answer = x / y;
    System.out.print(answer);
}

}

Output = 2.3333333333333335 (I understand the result, all variables defined as 'double')

Mike
  • 3
  • 2
  • It converts to `double` after all the operations have been done, not before. – Eli Sadoff Dec 26 '16 at 01:42
  • You need to gain a general understanding of the difference between integers and floating-point in computers, and how the type of variables influences the operations that are performed on them. In general, when you perform a binary operation on two numbers of type T the result will be of type T. If you want the operation to be carried out using a different type you must convert at least one of the operands (and perhaps both) to that type beforehand. – Hot Licks Dec 26 '16 at 01:46

4 Answers4

0

Dividing two integers will yield an integer (which is 2 in this case). It just so happens that you chose to store this integer in a double, so it is now represented as 2.0.

Saturn
  • 17,888
  • 49
  • 145
  • 271
0

The first code reads everything as int and gives an int. The second code reads two ints and gives the double of the ints (example: 10/5 = 2, but 2 in double is 2.0). In the end the third code reads everything as double so for example 70/30 (read as 70.0/30.0) = 2.33...

0

Let's get a closer look at how Java executes this line in the second piece of code:

answer = x / y;

First, Java sees a = operator so it knows that this is an assignment statement. To evaluate an assignment, evaluate the expression on the rignt then put the result into the variable on the left. Therefore, it evaluates the right hand side first.

x / y

Hmm... what could be the result of that? x is an int and y is an int and you have a / operator. I know the division operator can be applied to two int operands, so let me get the value of x and y. Ah! It's 70 / 30! Since it is an integer divided by an integer, the result must be an integer! The result is 2!

Now the assignment becomes:

answer = 2;

Java finds an integer on the right and a double variable on the left, so it converts 2 into a double and puts it in the variable.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

The Example 2, when compute answer = x / y, the first compute x / y and result is 2 then convert the double type 2.0.

lenzhao
  • 1
  • 1