Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Float Class | Methods, Example

Float class in Java is a wrapper class that wraps (converts) a value of primitive data type “float” in an object.

An object of Float Class contains a single float type field that store a primitive float number.

Float class was introduced in JDK 1.0. It is present in java.lang.Float package.

Java Float class declaration


The general declaration of Float class in Java is as follows:

public final class Float
  extends Number
     implements Comparable

Float class extends Number class and implements Serializable and Comparable interfaces. Number class extends Object class.

Constructors


Float class in Java defines three constructors that are as:

1. Float(float num): This form of constructor accepts a float number as its parameter and converts it into a Float class object. The general syntax to create Float class object to wrap float value is as:

float f = 15.999f;
Float obj = new Float(f);

Here, we are converting a primitive float value into Float Object. This is called boxing in java.

2. Float(double num): This form of constructor takes a double type number and then converts it into Float class object. The general syntax to create Float object to wrap double value is as:

double d = 123.989;
Float f = new Float(d);

3. Float(String str): This constructor accepts a parameter of String type and converts that string into a Float class object. The string contains a float value.

The general syntax to create a Float object by wrapping a string that contains a float value, as:

String str = "1230.99f";
Float f = new Float(str);

Important Float Class Methods in Java


In addition to methods inherited from the Number class and Object class, Java Float class also provides some useful important methods. They are as follows:

1. int compareTo(Float obj): This method compares the values of two Float class objects numerically. We call it using the below syntax:

int x = obj1.compareTo(obj2);

where, obj1 and obj2 are Float class objects.

  • If obj1 == obj2, this method returns 0.
  • If obj1
  • If obj1 > obj2, it returns a positive value.

2. boolean equals(Object obj): This method compares a Float object with another specified Float object obj. If both have the same content, the method returns true, otherwise returns false.

3. static float parseFloat(String str): This method returns the primitive float value contained in the string argument str.

4. String toString(): The toString() method converts Float object into String object and returns that String object. It returns a string form of Float object.


5. static Float valueOf(String str): It converts string str containing some float value into Float object and returns that Float object.

In other words, it returns a Float object holding a value given by the specified string.

6. static Float valueOf(float f): This method converts the primitive float value into a Float object. In other words, it returns a Float object representing the specified float value.

7. static int compare(float x, float y): It is used to compare two float values numerically.

8. static String toHexString(float f): This method converts a decimal float value f into hexadecimal number system. After converting, it returns that hexadecimal number as a string.


9. int hashCode(): It returns a hash code value for the Float object.

10. byte byteValue(): This method returns the byte value of this Float object after a narrowing primitive conversion.

11. short shortValue(): It returns the short value of this Float object after a narrowing primitive conversion.

12. int intValue(): This method returns an int value of this Float object after a narrowing primitive conversion.


13. long longValue(): It returns the long value of this Float object after a narrowing primitive conversion.

14. float floatValue(): This method returns the float value of this Float object.

15. float doubleValue(): This method returns the double value of this Float object after a widening primitive conversion.

16. static float max(float a, float b): This method returns the greater of two float values if we call it as Math.max.


17. static float min(float a, float b): This method returns the smaller of two float values as if by calling Math.min.

18. boolean isNaN(): It returns true if this Float value is a Not-a-Number (NaN), otherwise returns false.

19. static boolean isNaN(float v): It returns true if the specified number is a Not-a-Number (NaN) value, otherwise returns false.

20. static boolean isFinite(float f): This method returns true if the argument is a finite floating-point value, otherwise returns false (for NaN and infinity arguments).


21. boolean isInfinite(): This method returns true if this Float number is infinitely large in magnitude, otherwise returns false.

22. static boolean isInfinite(float v): It returns true if the specified value is infinitely large in magnitude, otherwise returns false.

Float class Methods Example Programs


Let’s take an example program based on the important methods byteValue(), shortValue(), intValue(), longValue(), floatValue(), and doubleValue() of Float class.

Program code 1:

package javaProgram;
public class FloatTest {
public static void main(String[] args) 
{
   Float x = 1230.55f; // Float value.
// Create a Float class object.
// Wrapping the Float value in the Float object. 
   Float f = new Float(x);
   byte b = f.byteValue();
   short s = f.shortValue();
   int i = f.intValue();
   long l = f.longValue();
   float fl = f.floatValue();
   double d = f.doubleValue();
	   
   System.out.println("byteValue(x): " +b);
   System.out.println("shortValue(x): " +s);
   System.out.println("intValue(x): " +i);
   System.out.println("longValue(x): " +l);
   System.out.println("floatValue(x): " +f);
   System.out.println("doubleValue(x): " +d);
 }
}
Output:
      byteValue(x): -50
      shortValue(x): 1230
      intValue(x): 1230
      longValue(x): 1230
      floatValue(x): 1230.55
      doubleValue(x): 1230.550048828125

Let’s create a Java program to compare two float values numerically using compare() method of Float class.

Program code 2:

package javaProgram;
public class FloatTest {
public static void main(String[] args) 
{
// Initializing two float values to be compared.
   Float f1 = 2345.55f;
   Float f2 = 234.55f;

// Call compare() method to compare two float values.
   if (Float.compare(f1, f2) == 0) {
      System.out.println("f1  =  f2");
   }
   else if (Float.compare(f1, f2)  f2");
   }   
 }
}
Output:
      f1 > f2

Let’s create a Java program to compare two different Float objects numerically using compareTo() method of Float class.

Program code 3:

package javaProgram;
public class FloatTest {
public static void main(String[] args) 
{
// Creating two objects of Float class and passing two float values.
   Float f1 = new Float(12.57f);
   Float f2 = new Float(10.689f);

// Calling compareTo() method to compare two Float objects.    
   int compareValue = f1.compareTo(f2);
   if (compareValue == 0)
      System.out.println("f1 and f2 are equal");
   else if (compareValue 
Output:
      f1 is greater than f2

Let’s create a Java program to compare a Float object with another specified Float object using equals() method of Float class.

Program code 4:

package javaProgram;
public class FloatTest {
public static void main(String[] args) 
{
// When two Float objects are different.	
   Float f1 = new Float(55.99f);
   Float f2 = new Float(88.88f);
   if (f1.equals(f2))
      System.out.println("f1 is equal to f2");
   else
      System.out.println("f1 is not equal to f2");

// When two Float objects are equal
    f1 = new Float(123f);
    f2 = new Float(123f);
   if (f1.equals(f2))
      System.out.print("f1 is equal to f2");
   else
      System.out.print("f1 is not equal to f2");
 }
}
Output:
      f1 is not equal to f2
      f1 is equal to f2

Let us write Java code to convert a string value into a primitive float value using parseFloat() method of Float class.

Program code 5:

package javaProgram;
public class FloatTest {
public static void main(String[] args) 
{
 String str = "99.99f";
 float f = Float.parseFloat(str);
 System.out.println("Primitive float value = " +(f + 20.55));
 System.out.println("String representation = " + (str + 20.55));
 }
}
Output: 
      Primitive float value = 120.53999786376953
      String representation = 99.99f20.55

Let us write a Java program to convert Float object into String object and store the returned String object into variable str of String type.

Program code 6:

package javaProgram;
public class FloatTest {
public static void main(String[] args) 
{
  Float f = 77.99f;
  Float obj = new Float(f);
// Call to String() method to convert Float object into String object.
   String str = obj.toString();
   System.out.println("String representation: " + (str + 10101));
   System.out.println("Float value: " +(f + 10101));
 }
}
Output:
      String representation: 77.9910101
      Float value: 10178.99

Let us write a Java program to convert a string containing a float value into Float object using valueOf() method of Float class.

Program code 7:

package javaProgram;
public class FloatTest {
public static void main(String[] args) 
{
  String str = "44.99f";
  Float obj = new Float(str);
  Float f = obj.valueOf(str);
  System.out.println("Float value: " +f);
 }
}
Output:
      Float value: 44.99

In this tutorial, you learned a wrapper Float class in Java with some important example programs. Hope that you will have understood the basic concept of Float class object and practiced all programs.
Thanks for reading!!!

The post Java Float Class | Methods, Example appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Java Float Class | Methods, Example

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×