HomeJava

Java for Testers – float data type

Java for Testers – float data type

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

In the previous article, I have explained about long data type.

In this article, I am going to explain and practically demonstrate float data type.

Java for Testers – float data type

float data type also stores the decimal values similar to double data type.

But the size of the float data type is 2 times smaller than the double data type.

i.e. float data type is single-precision 32 bit IEEE 754 floating-point and double data type is double-precision 64 bit IEEE 754 floating-point.

In general, we use double data type for storing the decimal values into a variable, but we may have to use float data type where we have to save memory.

Also, we have to add the letter f at the end of the value to make it a float type decimal literal.

For example:

In the above example, we have declared the variable i with float data type, and hence we are able to store the decimal values like 9.36 into the variable.

Also, observe that we have added a letter f to the value 9.63 to from float type integer literal value 9.63f.

Follow the below steps for practically implementing float data type in Java:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable, declare the variable with float data type and assign the variable with a decimal value 9.36f as shown below:

float data type Java - float

3) Now print the value stored in the variable by printing the variable using the print statement as shown below:

float data type Java - float print

4) Save the Project and click on the ‘Run’ button.

Observe that the Program got executed and the decimal value 9.36f stored in the variable i  got printed as shown below:

float data type Java - float printed

Copy the above-explained code from the below section

Here concludes this article.

In the next article, I will explain the remaining things about data types.

Next Steps:

  • > To learn more about Java, continue to the next post (Click on Next Post link below)
  • > Check complete Java Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below:

Happy Learning 🙂

Arun Motoori (www.QAFox.com)

On a mission to help the Testing Community in all possible ways.

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *