Results 1 to 2 of 2
  1. #1
    javanub is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default Float vs. Double

    I've searched around and can't find out why I get an error in the following program:

    --------------------------------------
    class Project21 {
    public static void main(String args[]) {
    double dtest;
    float ftest;
    dtest=7.2;
    ftest=7.2;
    System.out.println(dtest);
    System.out.println(ftest);
    }
    }
    --------------------------------------

    I get the following error when I try to compile it:

    Project21.java:6: possible loss of precision
    found : double
    required: float
    ftest=7.2;
    ^
    1 error

    From what I understand Float and Double both represent fractional numbers with Float being 32 bits and Double being 64 bits. Shouldn't they both be able to handle a "7.2"?

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    You can declare float variable and make it double without casting,it should be :

    Java Code:
    class Project21 {
    	public static void main(String args[]) {
    		double dtest;
    		float ftest;
    		dtest=7.2;
    		ftest=7.2F;
    		System.out.println(dtest);
    		System.out.println(ftest);
    	}
    }

Similar Threads

  1. Float and double to binary
    By rosh72851 in forum Advanced Java
    Replies: 3
    Last Post: 09-24-2008, 06:29 AM
  2. Put float value to double
    By chamalsl in forum New To Java
    Replies: 3
    Last Post: 09-20-2008, 04:52 AM
  3. Float to String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 05:08 PM
  4. Replies: 2
    Last Post: 11-16-2007, 01:24 PM
  5. Class float
    By Peter in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:17 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
  •