Categories: Java

Float class in Java

The Float class is a wrapper class for the primitive type float. It contains several methods for dealing with float values effectively, such as converting them to string representations and vice versa. A Float object can only hold one float value.

Constructor of float wrapper class:

There are mainly two constructors to initialize a Float object-

public Float(Float d)
public Float(String s) throws NumberFormatException
Methods of Float wrapper class:
MethodsDescription
int compareTo(Float b)– Returns zero if the invoked Float object is the same as the value of b.
– Returns a positive value if the invoked Float object is greater than or equal to b.
– Returns a negative value if the invoked Float object is less than or equal to b.
boolean equals(Object ob)Returns true if the invoked Float object has the same value as ob, otherwise false.
static float parseFloat(String s)If String s can be converted to a valid float value, it returns a primitive float value.
static Float valueOf(Float b)After converting a primitive float value, b, to a Float object.
static Float valueOf(String s)Returns a Float object after converting it from a String s.
short shortValue()Returns a primitive short value after converting a Float object to it.
byte byteValue()Returns a primitive byte value after converting a Float object to it.
int intValue()Returns a primitive int value after converting a Float object to it.
long longValue()Converts a Float object to a primitive long value and returns it.
float floatValue()Returns a primitive float value after converting a Float object to it.
double doubleValue()Returns a primitive double value after converting a Float object to it.
boolean isFinite()If the argument is a finite floating-point value, isFinite() returns the Boolean value ‘true’.
boolean isInfinite()If this float value or the specified number has an infinitely large magnitude, this function returns the Boolean value ‘true.’
Example: Converting Byte object’s value to any numeric primitive type
import java.util.*;

class Coderz
 {
  public static void main(String []args)
   {
    Float f = new Float(45.0); //Converting an int value argumet to    wrapper Float object

    System.out.println("Value in wrapped object f : "+ f);

    byte b = f.byteValue();  //Returns a primitive byte value out of a wrapped Float object
    short s= f.shortValue(); //Returns a primitive short value out of a wrapped Float object
    int i = f.intValue();  //Returns a primitive int value out of a wrapped Float object
    long l = f.longValue();  //Returns a primitive long value out of a wrapped Float object
    float ff = f.floatValue(); //Returns a primitive float value out of a wrapped Float object
    double d = f.doubleValue(); //Returns a primitive double value out of a wrapped Float object

   System.out.println("byte value of Float object f : " + b);
   System.out.println("short value of Float object f : " + s);
   System.out.println("int value of Float object f : " + i);
   System.out.println("long value of Float object f : " + l);
   System.out.println("float value of Float object f : " + ff);
   System.out.println("double value of Float object f : "+ d);

  }
}
Output:
Value in wrapped object f : 45.0
byte value of Float object f : 45
short value of Float object f : 45
int value of Float object f : 45
long value of Float object f : 45
float value of Float object f : 45.0
double value of Float object f : 45.0
Example: comparing two Float objects values
class Coderz
 {
  public static void main(String []args)
   {
    Float f1 = new Float("10.5");   //Constructor accepting String value
    Float f2 = new Float(10.5);    //Constructor accepting primitive float value

    System.out.println("Value in b1 = "+f1);
    System.out.println("Value in b2 = "+f2);

    System.out.println("Invoking "+f1 +"to compare with"+ f2+" : "+ f1.compareTo(f2));

    Float f3 = new Float("20.5");
    Float f4 = new Float(15.5);

    System.out.println("Value in "+f3 +"= "+f3);
    System.out.println("Value in "+f4 +"= "+f4);

    System.out.println("Invoking "+f3 +"to compare with"+ f4 +": "+ f3.compareTo(f4));

    System.out.println("Invoking "+f4 +"to compare with"+ f3 +": "+ f4.compareTo(f3));

   }
}
Output:
Value in b1 = 10.5
Value in b2 = 10.5
Invoking 10.5 to compare with10.5 : 0
Value in 20.5= 20.5
Value in 15.5= 15.5
Invoking 20.5 to compare with15.5: 1
Invoking 15.5 to compare with20.5: -1
Exception when converting a String to a Float object:
class Coderz
{
  public static void main(String []args)
  {
    Float b1 = new Float("100t"); //Passing a String value that cannot be convered to a Float object.
  }
}
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "100t"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)at java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.base/java.lang.Float.parseFloat(Float.java:455)
 at java.base/java.lang.Float.<init>(Float.java:554)
 at Coderz.main(Coderz.java:5)

When we pass a String value to the constructor of the Float class or its method parseFloat() that is not a legal float value or is out-of-range, a NumberFormatException is thrown.

Note: also read about the Short class in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago