Results 1 to 9 of 9
  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default 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

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    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 06:23 PM.

  3. #3
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    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."
    Use your own reasoning.
    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. #4
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default 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.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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!

  6. #6
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    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 03:06 PM.

  7. #7
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Lightbulb '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. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by lala View Post
    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 :)
    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!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Is there some standard set of questions going round at the moment?
    This exact question was asked earlier this week...

Similar Threads

  1. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 03:42 PM
  2. Type conversion
    By hannes in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 11:29 AM
  3. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  4. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  5. runtime type conversion
    By sardare in forum Advanced Java
    Replies: 3
    Last Post: 08-18-2008, 08:51 AM

Posting Permissions

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