1. Member
Join Date
Oct 2010
Posts
80
Rep Power
0

## Type Conversion Confusion

Greetings all.
Will someone please explain the difference in the final results in the following two expressions.

(int) (7.8 + (double) (15) / 2)

(int) (7.8) + (double) (15.0) / 2.0)
(int) (7.8) + (double) 7.5 = 15.3

Book Ans:
(int) (7.8) + (double) (15) / 2)
= (int) (7.8 + 7.5) where did type double go?
= 15
Why:I thought the rule stated that "during calculation the integer is treated temporarily as a floating-point number, & then the operator is evaluated. The RESULT is a floating-point number."

Problem2:
(int) (7.8 + (double) (15 / 2))
Book Ans:
(int) (7.8 + 7.0) again, where did type double go? If data type is int, why the decimals?
= (int) (14.8)
= 14

I thought (int) 5.5 evaluated to the decimal being dropped. (??):confused:
Please clear the fog. What am I missing?

2. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0
Book Ans:
(int) (7.8) + (double) (15) / 2)
= (int) (7.8 + 7.5) where did type double go?
= 15
What happens is this:

(int) (7.8) + (double) (15) / 2)the "(double)" turn the 15 into a 15.0
=(int) (7.8) + (15.0)/ 2) deviding a double by an int gives a double
= (int) (7.8 + 7.5)just adding 2 doubles here, that gives another double
=(int) (15.3)casting to an int chops off the decimals
=15
Last edited by imorio; 01-05-2011 at 07:23 PM.

3. Member
Join Date
Oct 2010
Posts
63
Rep Power
0
Book Ans:
(int) (7.8) + (double) (15) / 2)
= (int) (7.8 + 7.5) where did type double go?
= 15
Why:I thought the rule stated that "during calculation the integer is treated temporarily as a floating-point number, & then the operator is evaluated. The RESULT is a floating-point number."
Java Code:
``` public static void main(String[] args) {

System.out.println ("(int)(7.8) = " + (int) (7.8));
System.out.println ("(double)((15.0)/2.0) = " + (double)((15.0) /2.0));
System.out.println ("(double)((15  )/2  ) = " + (double)((15.0) /2.0));
System.out.println ("(int)(7.8) + (double)((15.0) /2.0)) = " + ((int) (7.8) + (double)((15.0) /2.0)));
System.out.println();
System.out.println ("(int)(7.8) + (double)((15  ) /2  )) = " + ((int) (7.8) + (double)((15  ) /2  )));
}

OUTPUT:
(int)(7.8) = 7
(double)((15.0)/2.0) = 7.5
(double)((15  )/2  ) = 7.0
(int)(7.8) + (double)((15.0) /2.0)) = 14.5

(int)(7.8) + (double)((15  ) /2  )) = 14.0```

4. Member
Join Date
Oct 2010
Posts
80
Rep Power
0

## Type Conversion Confusion

Thanks, imorio & hoscomp.

This statement is what tripped me up.
=(int) (7.8) + (15.0)/ 2) this is still type double

=(int) (7.8) + (double) 7.5 = 15.3.

5. Senior Member
Join Date
Mar 2009
Posts
552
Rep Power
9
But the expression isn't (int) (7.8) + (double) 7.5. Its (int)(7.8+7.5) which does come out as 15, as the type is cast to int after the addition. Also, the double cast is unnecessary due to the precedence of typecasts, and because and expression like double <operator> int always results in a double.

The full steps:

(int) (7.8 + (double) 15.0/2)
(int) (7.8 + 7.5)
(int) (15.3)
15

As a side note (int) (7.8) + (double) 7.5 isn't 15.3. Its 14.5, because casts are applied before operators, so it becomes 7 + 7.5.

EDIT: The above is still relevant, but what you're also missing is that in (int)(7.8+7.0) (second problem, though it applies to the first as well), the (int) doesn't mean that the type of the values is int - it just means that the result will be cast to an int. The values are still of type double.
Last edited by Singing Boyo; 01-06-2011 at 03:06 AM.

6. Member
Join Date
Oct 2010
Posts
63
Rep Power
0
But the expression isn't (int) (7.8) + (double) 7.5. Its (int)(7.8+7.5)
That is correct. I only looked at the Book Ans: part and dropped the extra right paren.

This statement is what tripped me up.
=(int) (7.8) + (15.0)/ 2) this is still type double
Would be
=(int) (7.8) + (15.0)/ 2
=(int) (7.8) + 7.5
= 7 + 7.5
= 14.5
I think we have killed this horse.
Last edited by hosscomp; 01-06-2011 at 04:06 PM.

7. Member
Join Date
Oct 2010
Posts
80
Rep Power
0

## 'Type Conversion Confusion'‏

I get it! :)
I don't know what happened this morning. But I get it.
Maybe it's the fresh, softly falling snow. The quiet. Or the name Singing Boyo that brought a smile to my face.

Many, many thanks to Imorio, Hosscomp, & Singing Boyo <smile>

P.S. Sometimes I make things way to hard.

8. Senior Member
Join Date
Mar 2009
Posts
552
Rep Power
9
Originally Posted by lala
P.S. Sometimes I make things way to hard.
It happens while programming. I took forever to wrap my head around several concepts, notably what methods and classes were. But once you get it, you really do get it, and I'm glad I could help :)

9. Moderator
Join Date
Apr 2009
Posts
13,324
Rep Power
23
Is there some standard set of questions going round at the moment?
This exact question was asked earlier this week...

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•