0

It is understandable that :

  public int divide() {

  return 23/4; /* gives 5 , implicitly typecasting 5.75 to 5 ,due to 23 and 4 being
                  both integers ,at least I think this is */
  }

and also ,

public double divide() {

  return 23.0/4.0; /*gives 5.75 , since it takes 23.0 and 4.0 to be float and not int*/

}

I have a code:

public double divide() {

  double intger = 23/4;
  return intger;

}

In it ,why is it so that even when I am assigning 23/4 to a double , what I get is just 5.0 ? And also please check if I understood the first two correctly .

Thanks.

EDIT:

I got my answer . Thanks to all those who helped.

Gaurang Tandon
  • 5,704
  • 9
  • 37
  • 73

6 Answers6

4

23/4 is an int which is then implicity converted to double.

Edit: Try this out

System.out.println(23/4);
System.out.println((double)(23/4));
System.out.println((double)23/(double)4);
rocketboy
  • 8,990
  • 1
  • 31
  • 36
2

int / int = int So you assigned an integer to a double. Firstly, you should get a double value.

Try this one:

public double divide() {

  double toret = 23/4.0;
  return toret;

}
  • So , why does this return 5.75 , both have to be double for this I thought ? – Gaurang Tandon Aug 15 '13 at 06:30
  • Because int / double or double / int returns a double value. –  Aug 15 '13 at 06:30
  • Never knew this thing , possibly because the second int implicitly typecasts to double ? – Gaurang Tandon Aug 15 '13 at 06:33
  • 1
    If one them is a double value, it returns a double value because you can assign an integer value to a double value. However, you cannot assign a double to an integer, you should cast it. –  Aug 15 '13 at 06:36
2

The expression 23/4 is integer division, and gives an integer result.

So it evaluates to 5, which is then assigned to your double.

andy256
  • 2,647
  • 2
  • 11
  • 19
2

double integer = 23/4;

step by step:

  1. 23/4 - integer division, since both arguments are int; result is 5
  2. double intger = 5; a conversion needed, since left value is double, while right value is int; int to double conversion can be implicit; so intger = 5.0

if you want to have float point division - just divide float points:

  double intger = 23.0 / 4.0;
Dmitry Bychenko
  • 149,892
  • 16
  • 136
  • 186
1

23/4 is an integer.. you have performed division between two integers so the result is an integer..5, which is stored as double 5.0..

if you wish to get a double value returned use:

public double divide() {

  double intger = 23.0/4.0;
  return intger;

}
Abhishek
  • 846
  • 2
  • 8
  • 22
1

In your first example, 23/4 returns 5 because that is what integer division does: p/q returns the largest integer i such that i*q <= p. There is no casting going on here!

In your second example, 23.0/4.0 returns 5.75 because this is how floating-point division works: when p and q are floats, p/q returns the most precise representation of f available such that f * q = p.

In a case you don't consider, 23.0/4 would also return 5.75. In this case, there is casting. Since p and q are of different types, float and int, the lower type is promoted without loss of precision and the division proceeds as if p and q were of the higher type all along.

In the case you're concerned about, it is always possible to use an int to assign a value to a higher-typed variable (long, float, or double). What happens here is integer division, followed by promotion.

In order to force floating-point division, cast one of p or q to floating-point:

public double divide(int p, int q) {

  return  (double)p/q;
}
Jon Kiparsky
  • 6,854
  • 2
  • 20
  • 37