Results 1 to 9 of 9
Thread: Type Conversion Confusion
- 01-05-2011, 05:47 PM #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)
My Answer:
(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?
Thanks In Advance
- 01-05-2011, 06:21 PM #2
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
What happens is this:Book Ans:
(int) (7.8) + (double) (15) / 2)
= (int) (7.8 + 7.5) where did type double go?
= 15
(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
=15Last edited by imorio; 01-05-2011 at 06:23 PM.
- 01-05-2011, 08:02 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
Use your own reasoning.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
- 01-05-2011, 08:41 PM #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
Now i'm adding:
=(int) (7.8) + (double) 7.5 = 15.3.
- 01-06-2011, 02:04 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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 02:06 AM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 01-06-2011, 02:55 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
That is correct. I only looked at the Book Ans: part and dropped the extra right paren.But the expression isn't (int) (7.8) + (double) 7.5. Its (int)(7.8+7.5)
Would beThis statement is what tripped me up.
=(int) (7.8) + (15.0)/ 2) this is still type double
I think we have killed this horse.=(int) (7.8) + (15.0)/ 2
=(int) (7.8) + 7.5
= 7 + 7.5
= 14.5Last edited by hosscomp; 01-06-2011 at 03:06 PM.
- 01-06-2011, 03:07 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.
- 01-07-2011, 12:13 AM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 01-07-2011, 08:59 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
Similar Threads
-
Confusion here @@' Help!
By pleasurelyours in forum New To JavaReplies: 7Last Post: 06-09-2010, 03:42 PM -
Type conversion
By hannes in forum New To JavaReplies: 2Last Post: 12-18-2009, 11:29 AM -
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-06-2008, 06:24 PM -
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09-17-2008, 10:43 AM -
runtime type conversion
By sardare in forum Advanced JavaReplies: 3Last Post: 08-18-2008, 08:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks