Type Conversion Confusion

• 01-05-2011, 06:47 PM
lala
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?
• 01-05-2011, 07:21 PM
imorio
Quote:

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
• 01-05-2011, 09:02 PM
hosscomp
Quote:

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."
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```
• 01-05-2011, 09:41 PM
lala
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.
• 01-06-2011, 03:04 AM
Singing Boyo
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.
• 01-06-2011, 03:55 PM
hosscomp
Quote:

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.

Quote:

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

Would be
Quote:

=(int) (7.8) + (15.0)/ 2
=(int) (7.8) + 7.5
= 7 + 7.5
= 14.5
I think we have killed this horse.
• 01-06-2011, 04:07 PM
lala
'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.
• 01-07-2011, 01:13 AM
Singing Boyo
Quote:

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 :)
• 01-07-2011, 09:59 AM
Tolls
Is there some standard set of questions going round at the moment?
This exact question was asked earlier this week...