# Double floatValue() in Java with Examples

When it comes to dealing with precision-based calculations, Java provides a myriad of tools and functions. One such functionality is the handling of floating-point numbers, particularly with the Double and Float wrapper classes. Today, we are going to unravel the mysteries around the Double and Float Value() method in Java.

## Syntax

The Value() function in the Double and Float classes in Java is defined as follows:

public double doubleValue()
public float floatValue()


## Explanation of Syntax

These techniques are a piece of the Twofold and Float covering classes. The doubleValue() technique changes over the Twofold item into a twofold crude, while floatValue() changes over the Float object into a float crude. Both these strategies acknowledge no boundaries.

## Algorithm

Here's the underlying algorithm at work when you use these methods −

• Declare and initialize a Double or Float object with a numeric value.

• Call the doubleValue() or floatValue() method on the object.

• The method returns the primitive double or float value of the object.

## Approach 1: Using Value() in mathematical operations

You can use the Value() method to convert Double or Float objects to primitive data types, enabling you to use them in mathematical operations.

### Example

public class Main {
public static void main(String[] args) {
Double obj = new Double(20.5);
double primitive = obj.doubleValue();
double result = primitive / 2; // mathematical operation
System.out.println(result); // prints 10.25
}
}


### Output

10.25


### Explanation

This Java program demonstrates the doubleValue() method of the Double wrapper class. It begins by creating a Double object, obj, with a value of 20.5. Then doubleValue() is invoked on obj, transforming it into a primitive double, which is stored in primitive. A mathematical operation, specifically dividing primitive by 2, is performed, with the result stored in the result. The System.out.println(result) line then outputs the result of the division (10.25) to the console. This example highlights the use of the doubleValue() method to convert Double objects into primitive double data types, enabling mathematical operations to be performed directly on the values.

## Approach 2: Using Value() in comparison operations

Value() can also be useful when you need to compare floating-point numbers.

### Example

public class Main {
public static void main(String[] args) {
Double obj1 = new Double(20.5);
Double obj2 = new Double(30.5);
boolean isEqual = obj1.doubleValue() == obj2.doubleValue(); // comparison operation
System.out.println(isEqual); // prints false
}
}


### Output

false


### Explanation

This Java program illustrates the comparison of Double objects using the doubleValue() method. Initially, two Double objects obj1 and obj2 are created and initialized with values of 20.5 and 30.5 respectively. The doubleValue() method is used to convert these Double objects into primitive double values. These double values are then compared using the equality operator (==). The result of this comparison is stored in the boolean variable isEqual. Since 20.5 is not equal to 30.5, isEqual is false. The System.out.println(isEqual) statement prints false to the console. This code demonstrates how you can compare two Double objects in Java, which is an essential task in many programming scenarios.

## Approach 3: Using Value() in conditional operations

Value() is also handy when working with conditional statements.

### Example

public class Main {
public static void main(String[] args) {
Float obj = new Float(20.5f);
if (obj.floatValue() > 20.0f) {
System.out.println("Greater than 20"); // prints "Greater than 20"
}
}
}


### Output

Greater than 20


### Explanation

This Java program demonstrates the use of the floatValue() method of the Float wrapper class within a conditional statement. First, a Float object obj is created with a value of 20.5f. Next, the floatValue() method is used to convert the Float object into a primitive float. This float value is then compared to 20.0f within an if statement. In the event that obj's float esteem is more prominent than 20.0f (which it is, since 20.5f is bigger than 20.0f), the program prints the string "More noteworthy than 20" to the control center. This is a typical use instance of the floatValue() strategy, permitting a Float object to be straightforwardly utilized in a contingent activity, exhibiting the power and adaptability of Java's covering classes.

## Approach 4: Using Value() in an array of objects

The Value() method can also be employed when dealing with an array of Double or Float objects.

### Example

public class Main {
public static void main(String[] args) {
Double[] objArray = {new Double(10.5), new Double(15.5), new Double(20.5)};
for (Double obj : objArray) {
System.out.println(obj.doubleValue()); // prints 10.5, 15.5, 20.5
}
}
}


### Output

10.5
15.5
20.5


### Explanation

The provided Java program demonstrates the use of the doubleValue() method in conjunction with an array of Double objects. In this program, an array, objArray, is created and populated with three Double objects holding values of 10.5, 15.5, and 20.5, respectively. The program then uses an enhanced for loop to iterate over each Double object in the array. Inside the loop, the doubleValue() method is invoked on the current Double object, converting it into a primitive double data type. The value is then printed to the console. As a result, when the program runs, it will print out 10.5, 15.5, and 20.5 sequentially. This program demonstrates how the doubleValue() method can simplify handling arrays of Double objects.

## Conclusion

The Worth() strategies for the Twofold and Float covering classes in Java are useful assets for managing drifting point numbers. They give a basic, clear method for changing over Twofold and Float objects into twofold and float natives, making these information types undeniably more adaptable and simple to utilize. Whether you're performing numerical tasks, looking at numbers, offering restrictive expressions, or managing exhibits, the Worth() strategy makes your life as a Java developer that a lot more straightforward. Blissful coding!

Updated on: 31-Jul-2023

36 Views 