Results 1 to 6 of 6
  1. #1
    tokoolio is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Adding an integer to a double?

    int y = 3;
    double z= y+4.0;

    Will the right hand side of the bottom line return a double, or will this be an error? I know y is an integer, but I believe I heard something like if you if you write double z=5, then you'll get 5.0 as an output, but I am not sure.

    Basically will the code work?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by tokoolio View Post
    Basically will the code work?
    Yes. y+4 will work too....

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by tokoolio View Post
    Basically will the code work?

    yes, the result will be a double.

  4. #4
    tokoolio is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    What if I had int z= y+4.5; would it just return y+4? Or perhaps int z = double y, would y just be rounded down?

    Thanks btw.
    Last edited by tokoolio; 01-15-2011 at 08:42 PM.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    would it just return y+4?
    No.
    Or perhaps int z = double y, would y just be rounded down?
    Yes.
    :D

  6. #6
    parminder is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Presence of any double in a expression results in promotion of all the variables in that expression to a double.
    So yes the code will work returning a double value.

Similar Threads

  1. adding integer to the JPanel
    By navid in forum New To Java
    Replies: 2
    Last Post: 12-18-2010, 04:26 PM
  2. Replies: 2
    Last Post: 12-14-2010, 08:01 PM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. How to extract integer value out of a double value?
    By javaexplorer in forum AWT / Swing
    Replies: 4
    Last Post: 01-16-2010, 05:32 AM
  5. Adding a double element to a vector
    By peachyco in forum New To Java
    Replies: 5
    Last Post: 11-25-2007, 06:07 PM

Tags for this Thread

Posting Permissions

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