?>
profile image
Announcements
No new announcements.
mentora icon
bookmark
Table of Contents

    Understanding Float and Double Variables in Java

    In this lesson, we will learn, what is Float and what is Double variable in Java programming. We will also look at some examples and will take a quiz at the end.

    Float and Double Type Variables

    As described before, an integer variable value has no fractional (decimal) part. Java uses two types of variables float and double that can store a decimal number. They each offer a different level of precision (ability to store numbers after the decimal point) as outlined below.

    Video Explanation

    Click the button below to watch the explanation.

    video image
    • float - A float data type variable can store about a maximum of 7 digits of precision. The memory capacity of the float data type is 4 bytes and can store a number between range −3.4E+38 to 3.4E+38 (meaning -3.4 × 1038 to 3.4 × 1038).
    • double - A double data type variable can store about a maximum of 15 digits of precision. The memory capacity of the double data type is 8 bytes and can store a number between range -1.7E+308 to 1.7E+308 (meaning -1.7 × 10308 to 1.7 × 10308).
    Note

    The main difference between float and double data type is their memory capacity, precision and number range as outlined above.

    Syntax of Declaring Float and Double Variable in Java
    float variable_name;
    double variable_name;

    Here float is used for declaring Float data type and double is used for declaring Double data type. variable_name is the name of variable (you can use any name of your choice for example: a, b, c, alpha, etc.) and ; is used for line terminator (end of line).

    Now let's see some examples for more understanding.

    Example 1

    Declare a float variable x and a double variable y

    float x;
    double y;
    Example 2

    Declare a float variable x to assign decimal number 18.286 and a double variable y to assign a decimal number 16722.02675585.

    float x = 18.286f;
    double y = 16722.02675585;
    Note

    Here f is used to denote a float type value. If no f is used in a decimal number, then the number is treated as double type value.

    Example 3

    Declare 3 float variables a, b, c and 3 double variable x, y, z

    float a, b, c;
    double x, y, z;
    Example 4

    Declare 3 float variables a, b and c to assign the float value 41.23, 87.335 and 62.471 respectively after that declare 3 float variables x, y and z to assign the double value 19.2347, 22.20 and 54.458691027 respectively

    float a=41.23f, b=87.335f, c=62.471f;
    double x=19.2347, y=22.20, z=54.458691027;
    Example 5

    Declare a float variable x and double variable y and assign the float value 12.365 and double value 79.2358 in the separate line respectively.

    float x;
    double y;
    x = 12.365f;
    y = 79.2358;
    Example 6

    Declare a float variable x and double variable y and assign the float value 7.18 and double value 11.62254 after that change their values to 44.23 and 67.48612 respectively.

    float x=7.18f;
    double y=11.62254;
    x = 44.23f;          // now the new value of x is 44.23
    y = 67.48612;       // now the new value of y is 67.48612
    Note

    If we want to store an integer number in a float or double type variable, we can do so because an int has a smaller range compared to a float, and a float has a smaller range compared to a double data type in terms of their storage capacity for numbers. Therefore, an integer value can be stored in a float type variable, and a float value can be stored in a double type variable.

    float x=58;
    double y=76;

    The integer value 58 will be converted into float data type value and will be stored as 58.0. Similarly the integer value 76 will be converted into double data type value and will be stored as 76.0.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Test Your Knowledge
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.