Java - Float Class



Introduction

The Java 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.

Class Declaration

Following is the declaration for java.lang.Float class −

public final class Float
   extends Number
      implements Comparable<Float>

Field

Following are the fields for java.lang.Float class −

  • static int MAX_EXPONENT − This is Maximum exponent a finite float variable may have.

  • static float MAX_VALUE − This is a constant holding the largest positive finite value of type float, (2-2-23)·2127.

  • static int MIN_EXPONENT − This is minimum exponent a normalized float variable may have.

  • static float MIN_NORMAL − This is a constant holding the smallest positive normal value of type float, 2-126.

  • static float MIN_VALUE − This is a constant holding the smallest positive nonzero value of type float, 2-149.

  • static float NaN − This is a constant holding a Not-a-Number (NaN) value of type float.

  • static float NEGATIVE_INFINITY − This is a constant holding the negative infinity of type float.

  • static float POSITIVE_INFINITY − This is a constant holding the positive infinity of type float.

  • static int SIZE − This is the number of bits used to represent a float value.

  • static Class<Float> TYPE − This is the Class instance representing the primitive type float.

Class constructors

Sr.No. Constructor & Description
1

Float(double value)

This constructs a newly allocated Float object that represents the argument converted to type float.

2

Float(float value)

This constructs a newly allocated Float object that represents the primitive float argument.

3

Float(String s)

This constructs a newly allocated Float object that represents the floating-point value of type float represented by the string.

Class methods

Sr.No. Method & Description
1 byte byteValue()

This method returns the value of this Float as a byte (by casting to a byte).

2 static int compare(float f1, float f2)

This method compares the two specified float values.

3 int compareTo(Float anotherFloat)

This method compares two Float objects numerically.

4 double doubleValue()

This method returns the double value of this Float object.

5 boolean equals(Object obj)

This method compares this object against the specified object.

6 static int floatToIntBits(float value)

This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.

7 static int floatToRawIntBits(float value)

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.

8 float floatValue()

This method returns the float value of this Float object.

9 int hashCode()

This method returns a hash code for this Float object.

10 static float intBitsToFloat(int bits)

This method returns the float value corresponding to a given bit representation.

11 int intValue()

This method returns the value of this Float as an int (by casting to type int).

12 boolean isInfinite()

This method returns true if this Float value is infinitely large in magnitude, false otherwise.

13 static boolean isInfinite(float v)

This method returns true if the specified number is infinitely large in magnitude, false otherwise.

14 boolean isNaN()

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

15 static boolean isNaN(float v)

This method returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

16 long longValue()

This method returns value of this Float as a long (by casting to type long).

17 static float parseFloat(String s)

This method returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.

18 short shortValue()

This method returns the value of this Float as a short (by casting to a short).

19 static String toHexString(float f)

This method returns a hexadecimal string representation of the float argument.

20 String toString()

This method returns a string representation of this Float object.

21 static String toString(float f)

This method returns a string representation of the float argument

22 static Float valueOf(float f)

This method returns a Float instance representing the specified float value.

23 static Float valueOf(String s)

This method returns a Float object holding the float value represented by the argument string s.

Methods inherited

This class inherits methods from the following classes −

  • java.lang.Object

Example

The following example shows the usage of Float class to get float from a string.

package com.tutorialspoint;

public class FloatDemo {

   public static void main(String[] args) {

      // create a String s and assign value to it
      String s = "+120";

      // create a Float object f
      Float f;

      // get the value of float from string
      f = Float.valueOf(s);

      // print b value
      System.out.println( "Float value of string " + s + " is " + f );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Float value of string +120 is 120.0
Advertisements