Convert Java Float to Numeric Primitive Data Types



The Float wrapper class provides methods to convert a Float object into various numeric primitive data types like short, int, float, and double in Java. This is particularly useful when handling float values and representing them in different numeric forms.
This article will show two approaches to convert a Float object to numeric primitive types using the Float class methods.

Using Float Wrapper Class Methods

In this approach, we use predefined methods of the Float class, such as shortValue(), intValue(), floatValue(), and doubleValue(), to convert a Float object into various primitive types. These methods directly perform the required conversions.

  • shortValue(): Converts the Float object into a short type. The fractional part is truncated.
  • intValue(): Converts the Float object into an int. The value is truncated to the nearest integer.
  • floatValue(): Converts the Float object into a float type. This retains the original value.
  • doubleValue(): Converts the Float object into a double. This expands the precision without data loss.

Example

Below is an example of converting Java float to numeric primitive data types using float wrapper class methods ?

public class Demo {
   public static void main(String args[]) {
      Float ob = new Float("29.35");
      // numeric primitive data types
      short val1 = ob.shortValue();
      System.out.println(val1);
      int val2 = ob.intValue();
      System.out.println(val2);
      float val3 = ob.floatValue();
      System.out.println(val3);
      double val4 = ob.doubleValue();
      System.out.println(val4);
   }
}

Output

29
29
29.35
29.350000381469727

Using Type Casting

In this approach, we directly use type casting to convert the Float object to various primitive data types. This approach is simpler but requires explicit casting for types like short and int.

  • Type Casting: Explicitly converts the float value to short and int using (short) and (int) respectively.
  • Auto-Unboxing: Java automatically converts the Float object to its corresponding primitive types (float and double) where required.

Example

Below is an example of converting Java float to numeric primitive data types using type-casting ?

public class Demo {
   public static void main(String args[]) {
      Float ob = new Float("29.35"); // Creating a Float object with a value
      
      // Convert to numeric primitive types using type casting
      short val1 = (short) ob.floatValue();
      System.out.println("Short value: " + val1);
      
      int val2 = (int) ob.floatValue();
      System.out.println("Int value: " + val2);
      
      float val3 = ob; // Auto-unboxing Float to float
      System.out.println("Float value: " + val3);
      
      double val4 = ob; // Auto-unboxing Float to double
      System.out.println("Double value: " + val4);
   }
}

Output

Short value: 29
Int value: 29
Float value: 29.35
Double value: 29.35

Comparison Between the Two Approaches

Aspect
Wrapper Methods
Type Casting
Method
Uses shortValue(), intValue(), etc.
Uses explicit type casting and auto-unboxing.
Readability Clear and descriptive due to named methods.
Less descriptive, and relies on casting syntax.
Flexibility
Allows direct conversions for various types.
Requires manual handling for type conversion.
Performance
Slightly slower due to method calls.
Slightly faster since casting avoids method calls.
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-30T19:14:14+05:30

491 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements