This discussion is archived
5 Replies Latest reply: Mar 27, 2013 1:53 AM by EJP RSS

Compatible & Incompatible types

967983 Newbie
Currently Being Moderated
Why is that I can assign a integer value to a double variable, but cannot assign a double value to an integer variable? Aren't double and integer compatible types? Isn't there a concept of truncation in java? I searched the net but in vain. I couldn't find the exact answer I was looking for. So I am posting in this forum.

PS: I understand it is not possible to assign an integer to a string since they are incompatible types.

Edited by: 964980 on Mar 26, 2013 9:29 PM
  • 1. Re: Compatible & Incompatible types
    rp0428 Guru
    Currently Being Moderated
    >
    Why is that I can assign a integer value to a double variable, but cannot assign a double value to an integer variable?
    >
    Because an integer value IS a double value but a double value is NOT an integer value.

    And once again you don't bother to post information you have about the question you are asking.

    How do you know you 'cannot assign a double value to an integer variable'? Did you try it? How did you try it? What code did you use? What was your 'double value'? What format did you use?

    If you tried it and it failed why didn't you post the code you used and the exact error message you got so people can SEE what it is you are doing and HOW you are doing it.

    You will get much better help if you provide ALL of the information you have about the problem you are asking about.

    Don't just say "why can't I do this". Provide the example code and say "I used this code to assign a double value to an integer variable and this is the error message that I got". Then post the exact error message.
  • 2. Re: Compatible & Incompatible types
    EJP Guru
    Currently Being Moderated
    Why is that I can assign a integer value to a double variable
    Because it's a widening conversion that doesn't lose any information. See JLS #5.1.2.
    but cannot assign a double value to an integer variable?
    Because it's a narrowing conversion that might lose information. You can do it as long as you write an explicit typecast. See JLS #5.1.3.
    Aren't double and integer compatible types?
    No, see above.
    Isn't there a concept of truncation in java?
    Yes, see above.

    @rp0428 I don't see what good asking for the code and the error message does, in a fundamental case like this. It doesn't change the answer, and your own answer (is/isn't) wasn't too inspiring.
  • 3. Re: Compatible & Incompatible types
    967983 Newbie
    Currently Being Moderated
    Thanks for the replies.

    The two links don't talk about truncation. Can you please share some link regarding truncation? I searched in google but could not find any good site which talks about truncation.
  • 4. Re: Compatible & Incompatible types
    gimbal2 Guru
    Currently Being Moderated
    964980 wrote:
    Thanks for the replies.

    The two links don't talk about truncation. Can you please share some link regarding truncation? I searched in google but could not find any good site which talks about truncation.
    The second link IS about truncation, only different terminology is used (narrowing). Don't discard based on keywords, read it and understand it and only THEN draw conclusions.

    and if that article is nog clear to you (which is understandable), you are free to google for other articles that cover the same material. A research into typecasting will likely net you all the basic understanding you need.
  • 5. Re: Compatible & Incompatible types
    EJP Guru
    Currently Being Moderated
    The two links don't talk about truncation.
    What part of "A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range" isn't about truncation?

    Come off it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points