Results 1 to 7 of 7

Thread: [RESOLVED] Data type float

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Resolved [RESOLVED] Data type float

    Hi..

    Can you explain what the difference between two lines stated below is?

    float a = 10.125;
    float b = 10.125F;

    Actually what I want to know is the usefulness of “F”

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Data type float

    The first is a double literal, the second a float literal. With the F postfix, the compiler can warn you about lost precision in the literal, and it will help for overload resolution in functions overloaded for float and double.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: Data type float

    Quote Originally Posted by CornedBee
    The first is a double literal, the second a float literal. With the F postfix, the compiler can warn you about lost precision in the literal, and it will help for overload resolution in functions overloaded for float and double.

    Thanks for your comment,

    If I used the following line in an application it will gives complie error.


    float a = 10.125;

    Can you explain it, please.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Data type float

    Because the double is 64-Bit var, but the float is 32-bit, that's why you are getting this error: you are using 64bit value inside a 32bit variable.

    If you use the postfix "F" it will go away, because the java compiler will understand that the value is of type "float"
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: Data type float

    Quote Originally Posted by ComputerJy
    you are using 64bit value inside a 32bit variable. If you use the postfix "F" it will go away, because the java compiler will understand that the value is of type "float"

    Can you explain this statement further? I'm not clear what you are saying.

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Data type float

    any value that has a floating point is a double in java.
    The double is a 64bit value (8-bytes) but you are attempting to put the double value in a float variable, which can hold only 32bit value (4-bytes just like the integer), java now thinks you are attempting to put 8bytes inside a 4byte container and that's not possible, right?

    So if you use the literal "F" as a postfix of the value, Java will understand this is a float value not a double value, so now it knows the value will fit in the container.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Re: Data type float

    Quote Originally Posted by ComputerJy
    any value that has a floating point is a double in java.
    The double is a 64bit value (8-bytes) but you are attempting to put the double value in a float variable, which can hold only 32bit value (4-bytes just like the integer), java now thinks you are attempting to put 8bytes inside a 4byte container and that's not possible, right?

    So if you use the literal "F" as a postfix of the value, Java will understand this is a float value not a double value, so now it knows the value will fit in the container.

    Thanks a lot, now I got the point.

Posting Permissions

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



Click Here to Expand Forum to Full Width