New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL
See the Tutorial List

Java Float Class

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 a float, as well as other constants and methods useful when dealing with a float.

Declaration:

public final class Float extends Number implements Comparable<Float>

Below are the Methods of Float class and their example.

1. toString()

It returns a new String representing of specified float object. Syntax of the method is given below.

Syntax:

	
public String toString(float b)
	

Example:

Lets take an example to get string object of a float type. We used toString() method which is static so we can call it by using the class name.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F; 
		System.out.println("toString(a) = " + Float.toString(a)); 
	}
}
	

float-class

2. valueOf()

This method returns a Float instance representing the specified float value. This method should generally be used in preference to the constructor Float(float).

Syntax

	
public static Float valueOf(float b)
	

Example:

In this example, we are using valueOf() method that returns instance of Float class which represents the specified float type.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F;
		String b = "63"; 
		Float x = Float.valueOf(a); 
		System.out.println("valueOf(a) = " + x); 
        x = Float.valueOf(b); 
		System.out.println("ValueOf(b) = " + x);         
	}
}
	

float-class-example

3. parseFloat()

This method returns a float value of the specified string value. We can use it to get a float value from string type value.

Syntax:

	
public static float parseFloat(String val) throws NumberFormatException
	

Example:

Lets take an example in which we have a string type variable and getting its float value using the parseFloat() method.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F;
		String b = "63"; 
		float x = Float.parseFloat(b); 
		System.out.println("parseFloat(b) = " + x);        
	}
}
	

float-class-example

4. byteValue()

This method is used to get a primitive type float value from Float object. It returns the numeric value represented by this object after conversion to type float.

Syntax

	
public byte byteValue()
	

5. shortValue()

This method returns the value of this Float as a short after a widening primitive conversion.

Syntax

	
public short shortValue()
	

6. intValue()

The intValue() method returns the value of this Float as a primitive int type after a widening primitive conversion. Syntax of this method is given below.

Syntax

	
public int intValue()
	

7. longValue()

The longValue() method returns the value of this Float type as a long type after a widening primitive conversion. Syntax of this method is given below.

Syntax

	
public long longValue()
	

8. doubleValue()

It returns the value of this Float type as a double type after a widening primitive conversion. Syntax of this method is given below.

Syntax

	
public double doubleValue()
	

9. floatValue()

This method is used to get value of this Float type as a float type after a widening primitive conversion. Syntax of this method is given below.

Syntax:

	
public float floatValue()
	

Example:

Lets take an example to convert float type to int, long and float type values. In this example, we are using intValue(), floatValue(), doubleValue() methods.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F;
		Float obj = new Float(a); 
		System.out.println("bytevalue(obj) = " + obj.byteValue()); 
		System.out.println("shortvalue(obj) = " + obj.shortValue()); 
		System.out.println("intvalue(obj) = " + obj.intValue()); 
		System.out.println("longvalue(obj) = " + obj.longValue()); 
		System.out.println("doublevalue(obj) = " + obj.doubleValue()); 
		System.out.println("floatvalue(obj) = " + obj.floatValue()); 
	}
}
	

float-class-example-2.JPG

10. hashCode()

This method is used to get hash code of a float value. It returns an int value of float object.

Syntax:

	
public inthashCode()
	

Example:

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F;
		Float obj = new Float(a); 
		int x = obj.hashCode(); 
		System.out.println("hashcode(x) = " + x); 
	}
}
	

float-class-example

11. isNaN()

This method returns a boolean value either true or false. It returns true, if this Float value is a Not-a-Number (NaN), false otherwise.

Syntax:

	
public booleanisNaN()
	

Example:

Lets take an example to check whether the given float value is NaN or not. See the below example.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F;
		Float obj = new Float(a); 
		Float x = Float.valueOf(a); 
		System.out.println("isNaN(d) = " + x.isNaN());  
	}
}
	

float-class-example

12. isInfinite()

This method is used to check whether the float value is infinitely large in magnitude. It returns a boolean value either true or false. Syntax of this method is given below.

Syntax:

	
public booleanisInfinite()
	

Example:

We can use this method to check the range of Float value whether it lies under infinitely or not.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F;
		Float obj = new Float(a); 
		Float x = Float.valueOf(a); 
		x = Float.valueOf(Float.POSITIVE_INFINITY + 1); 
		System.out.println("Float.isInfinite(x) = " + Float.isInfinite(x.floatValue()));
	}
}
	

float-class-example

13. toHexString()

This method is used to get a hexadecimal string representation of the float argument. It takes a float type argument that would be convert into hexadecimal value. Syntax of this method is given below.

Syntax:

	
public static String toHexString(float val)
	

Example:

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F;
		Float obj = new Float(a); 
		System.out.println("Float.toString(a) = " + Float.toHexString(a));	
	}
}
	

float-class-example

14. floatToIntBits()

This method is used to get representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout. It takes a floating-point argument. Syntax of this method is given below.

Syntax:

	
public static intfloatToIntBits(float val)
	

Example:

In this example, we are using floattointbits() method that returns a bit layout of floating-point value.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F;
		Float obj = new Float(a); 
		int x = Float.floatToIntBits(a); 
		System.out.println("Float.floatToLongBits(a) = " + x); 
	}
}
	

float-class-example

15. floatToRawIntBits()

This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values. Syntax of this method is given below.

Syntax:

	
public static intfloatToRawIntBits(float val)
	

Example:

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
        float a = 72.05F;
		Float obj = new Float(a); 
		int x = Float.floatToRawIntBits(a); 
		System.out.println("Float.floatToRawIntBits(a) = " + x); 
	}
}
	

float-class-example

16. IntBitsToFloat()

This method is used to get the float floating-point value with the same bit pattern. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point "single format" bit layout. Syntax of this method is given below.

Syntax:

	
public static float IntBitsToFloat(long b)
	

Example:

Lets take an example to understand the intbitstofloat() method that returns floating-point value.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
		int a = 72;
		float y = Float.intBitsToFloat(a); 
		System.out.println("Float.intBitsToFloat(a) = " + y); 
	}
}
	

float-class-example

17. equals()

The equals() method compares an object to the specified object. It returns true if objects are same; false otherwise. Syntax of this method is given below.

Syntax:

	
public boolean equals(Object obj)
	

Example:

We are comparing two float objects using the equals method that returns true if both the objects are true.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
		float a = 25.34F;
		String b = "26";
        Float obj = new Float(a); 
        Float obj1 = new Float(b); 
		boolean x = obj.equals(obj1); 
		System.out.println("obj.equals(obj1) = " + x);
	}
}
	

float-class-example

18. compareTo()

This method is used to compare two float objects numerically. It returns 0, if the both float objects are equal. It returns less the 0, if one float object is less than argument object. It returns greater than 0, if one float object is numerically greater than the argument float object. Syntax of this method is given below.

Syntax:

	
public intcompareTo(Float b)
	

Example:

In this example, we are comparing two float objects using compareTo() method that compares two float objects numerically and returns a numeric value.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
		float a = 25.34F;
		String b = "26";
        Float obj = new Float(a); 
        Float obj1 = new Float(b); 
		int x = obj.compareTo(obj1); 
		System.out.println("obj.compareTo(obj1) = " + x);
	}
}
	

float-class-example

19. compare()

It is used to compare two float values numerically. The value returned is identical to what would be returned by. Syntax of this method is given below.

Syntax:

	
public static int compare(float x,float y)
	

Example:

We can use compare method to compare two float values. It returns 0 if both are equal else returns either negative or positive numerical value.

	
public class FloatDemo1
{ 
    public static void main(String[] args)  
	{ 
		float a = 25.34F;
		String b = "26";
        Float obj = new Float(a); 
        Float obj1 = new Float(b); 
		int x = Float.compare(obj, obj1); 
		System.out.println("compare(obj, obj1) = " + x);
	}
}
	

float-class-example