22

Program 1

int sum = 30;  
double avg = sum / 4; // result is 7.0, not 7.5 !!!

VS.

Program 2

int sum= 30   
double avg =sum/4.0 // Prints lns 7.5

Is this because the '4' in program 1 is acting as a literal integer? so 30/4 would give me 7. However since this data type is a double, we need to add a .0 to end. so '7.0'

Program 2 has 4.0 which is acting as a literal double. an int/double would always give double because it more precise. so we get '7.5'. I don't understand what double data type is doing to the result though.. it really doesn't need to do anything since the conditions of the double data type are already satisfied.(have the most precise result out of the computation).

Am I wrong? I encourage you to correct me! This is how I learn.. :)

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Your title suggests that the confusion is around the behaviour of `println`, whereas it seems to me that the question is really about the division operator. – Jon Skeet Sep 14 '15 at 18:58
  • `it really doesn't need to do anything since the conditions of the double data type are already satisfied.` ?? What does that mean ? – Suresh Atta Sep 14 '15 at 18:58
  • @JonSkeet It seems, his question is that `why the result is getting printed like that` :) – Suresh Atta Sep 14 '15 at 18:59

5 Answers5

22

In your first example:

int sum = 30;
double avg = sum / 4; // result is 7.0, not 7.5 !!!

sum is an int, and 4 is also an int. Java is dividing one integer by another and getting an integer result. This all happens before it assigns the value to double avg, and by then you've already lost all information to the right of the decimal point.

Try some casting.

int sum = 30;
double avg = (double) sum / 4; // result is 7.5

OR

int sum = 30;
double avg = sum / 4.0d; // result is 7.5
Ben M.
  • 2,370
  • 1
  • 15
  • 23
11

This is an integer division (because it involves two integers)

int sum = 30;  
double avg = (sum / 4); // result is 7

Integer division, will round down to the nearest integer.

However, this is a double division (because 4.0 is a double)

int sum= 30   
double avg = (sum/4.0) //  result is 7.5

This is the expected behaviour, and the conversion are all well defined. No need to guess. Take a look at the java docs about conversion.

A widening conversion of an int or a long value to float, or of a long value to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode

Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
7

In java, operations involving only integer types (int, long and so on) have integer results. This includes divisions.

Both 30 and 4 are integer values, so if you try to divide them, integer division is used.

But if you try either 30.0/4 or 30/4.0 then the result will be 7.5 because you will be using floating-point division.

The declared type of the variable avg has no influence on the result. The decimal part is lost during the division, and not when you assign the value to the variable.

Andrea Dusza
  • 2,080
  • 3
  • 18
  • 28
Pedro Affonso
  • 1,656
  • 15
  • 26
  • If I write, say, 3.0 in Java, would it not be considered a double? If I wanted a float type for 3.0 i would write 3.0f. correct? –  Sep 14 '15 at 19:06
  • Yes, that is correct. Double is a floating-point type (the name double stands for double-precision floating-point). 3.0 is a double constant and 3.0f is a float constant. – Pedro Affonso Sep 14 '15 at 19:08
0

enter image description here

Image taken from : http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/mixed.html

Refer above URL for more clear explanation.

K139
  • 3,654
  • 13
  • 17
0

PROGRAM 1 In Java, when you do a division between two integers, the result is an integer. So when you do sum/4, the result is the integer 7. Then you assign that integer to a double variable, so that 7 turns into 7.0.

PROGRAM 2 In Java, when you do a division between an integer and a double, the result is a double. So when you do sum/4.0, the result is the double 7.5. Then you assign that to a double variable, so it's still 7.5.

So the problem is not how println prints the variables, but how division works in Java

Tomas Ceruti
  • 136
  • 5