Results 1 to 3 of 3
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default type mismatch: cannot convert from double to float

    I have following line of code:

    Java Code:
    float average = 12.2;
    It gives me following error

    Java Code:
    Type mismatch: cannot convert from double to float
    I tried few things and was able to fix it using:

    Java Code:
    float average = 12.2f;
    I don't know why f is required with float value. Please explain.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The default in java is that a primitive with a decimal in it is a double. For the lower precision float type we need to specify with a trailing "F", "f" or cast the value to float.
    Same applies with int and long types using "L", "l" or long cast.
    See paragraph 3 under the Literals header on Primitive Data Types.

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

Similar Threads

  1. Replies: 7
    Last Post: 01-12-2008, 09:02 PM
  2. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM
  3. convert string to float
    By miss_dot in forum NetBeans
    Replies: 1
    Last Post: 11-14-2007, 11:26 PM
  4. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:48 AM
  5. Error with convert to Float
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 02:18 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
  •