In this example we shall show you how to use a float type in Java. The float data type is a single-precision 32-bit IEEE 754 floating point. Use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.

    To use a variable of float type one should type the float keyword in a variable,

as described in the code snippet below.

package com.javacodegeeks.snippets.basics;

public class FloatExample {
	public static void main(String[] args) {
		float f1 = 422.27f;
		float f2 = (float)1234/33;
		System.out.println("Value of float f1 is: " + f1);
		System.out.println("Value of float f2 is: " + f2);



Value of float f1 is: 422.27
Value of float f2 is: 37.39394

This was an example of how to use a float type in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button