Home » Core Java » Java Float Datatype Example

About Venkat-Raman Nagarajan

Avatar photo
Venkat works for a major IT firm in India and has more than a decade of experience working and managing Java projects for a banking client.

Java Float Datatype Example

In Java, the Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float.

In addition, this class provides several methods for converting a float to a String and a String to float, as well as other constants and methods useful when dealing with float. Let us look at some of the common methods used with an example.

1. Float datatype

what does float mean in java

Some of the commonly used methods of Float are:

  • int compareTo(Float anotherFloat): compares two Float objects numerically
  • double doubleValue(): Returns the value of this Float as a double after a widening primitive conversion
  • boolean equals(Object obj): Compares this object against the specified object
  • float floatValue(): Returns the float value of this Float object
  • int intValue(): Returns the value of this Float as an int after a narrowing primitive conversion
  • boolean isNaN(): Returns true if this Float value is a Not-a-Number (NaN), false otherwise
  • long longValue(): Returns value of this Float as a long after a narrowing primitive conversion
  • static float parseFloat(String s): Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float
  • String toString(): Returns a string representation of this Float object
  • static Float valueOf(String s): Returns a Float object holding the float value represented by the argument string s

2. Java Float data type example

Let us now look at an example using all the methods discussed above.FloatExample

public class FloatExample{
	public static void main(String args[]){
		float a = 10.25F;
		float b = 23.22F;
		Float aF = new Float(a);
		Float bF = new Float(b);
		// compareTo(Float)
		System.out.println("aF.compareTo(bF) :"+aF.compareTo(bF));
		// doubleValue()
		System.out.println("doubleValue(aF) :"+aF.doubleValue());
		// equals(Object)
		System.out.println("equals(Object) :"+aF.equals(bF));
		// floatValue()
		System.out.println("aF.floatValue :"+aF.floatValue());
		// intValue()
		System.out.println("bF.intValue :"+bF.intValue());
		// isNan()
		System.out.println("bF.isNan() :"+bF.isNaN());
		// longValue()
		System.out.println("bF.longValue :"+bF.longValue());
		// parseFloat
		System.out.println("parseFloat :"+Float.parseFloat("57.86"));

The output for the above class would be as follows:

aF.compareTo(bF) :-1
doubleValue(aF) :10.25
equals(Object) :false
aF.floatValue :10.25
bF.intValue :23
bF.isNan() :false
bF.longValue :23
parseFloat :57.86

3. Download the Source Code

You can download the full source code of this example here: Java Float Datatype Example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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


Notify of

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

Inline Feedbacks
View all comments