Home >Java >javaTutorial >Using example double longValue() function in Java
Java is a powerful object-oriented language that allows a high degree of control and precision over various data types. One such function is double long Value(), which is a key method used to convert a long value into a double. This article provides a comprehensive understanding of the Double long Value() method in Java, including its syntax, explanation, and different usage methods.
The syntax of the Double long Value() method is very simple −
public double doubleValue()
The method doubleValue() is an instance method that belongs to the Number class and is overridden in the Double class. It converts a Double object to the basic double type. This method does not accept any parameters and returns the double value of this Double object.
Double num = new Double(123.45); double d = num.doubleValue(); System.out.println(d);
In the above code, the Double object num is instantiated with a value of 123.45. Then call the doubleValue() method to convert num to the original double type.
Initialize the Double object.
Call the doubleValue() method on the object.
doubleValue() method returns the double value of the object.
Let's examine two different ways of using properties in Java Servlets.
In its simplest form, the doubleValue() method is used to convert a Double object to a double primitive.
public class Main { public static void main(String[] args) { Double num = new Double(789.32); double d = num.doubleValue(); System.out.println(d); } }
789.32
This is a simple Java program contained in a class called "Main". In this class, there is a method named "main". The "main" method serves as the entry point of the program.
Inside the "main" method, an object named "num" is created using the `Double` class in Java, which is a wrapper class of the primitive data type `double`. The `Double` object is initialized to a value of 789.32.
Then, call the "doubleValue()" method on the "num" object. This method is a built-in function in Java that converts a 'Double' object to the original 'double' data type. The output of the `doubleValue()` method is assigned to the `double` variable "d".
Finally, use the `System.out.println(d);` statement to print the value of "d" to the console.
Thus, this program shows the direct application of creating objects in Java and converting `Double` objects to `double` primitive data type. It highlights how to utilize the `doubleValue()` method and output the results to the console.
The doubleValue() method is particularly useful when performing mathematical operations that require double precision.
public class Main { public static void main(String[] args) { Double num1 = new Double(45.67); Double num2 = new Double(32.14); double result = num1.doubleValue() / num2.doubleValue(); System.out.println(result); } }
1.4209707529558184
This Java program is encapsulated in a class named "Main" and uses the doubleValue() method to perform division between two Double objects.
Inside the main method as the starting point of the program, two Double objects num1 and num2 are created, with values of 45.67 and 32.14 respectively. The Double class is a wrapper class for the original double data type in Java, allowing the double to be used as a complete object.
Subsequently, the doubleValue() method is called on num1 and num2. This method is an inherent part of the Double class in Java and is used to convert a Double object to the basic double data type. The results of these changes are then used in a division operation, the result of which is assigned to the double factor "result".
Finally, the program uses System.out.println(result) to print the value of "result" to the control center; explanation. So, this particular Java snippet features the use of the doubleValue() strategy in numerical tasks.
doubleValue() method can also be used when processing Double object arrays.
public class Main { public static void main(String[] args) { Double[] numArray = {new Double(10.1), new Double(20.2), new Double(30.3)}; double sum = 0; for (Double num : numArray) { sum += num.doubleValue(); } System.out.println(sum); } }
60.599999999999994
This runnable Java program, located in a class named "Main", demonstrates the use of the doubleValue() method when working with an array of Double objects.
In the main method, three elements −10.1, 20.2 and 30.3 are initialized using the array numArray of Double objects. This array represents a sequence of Double objects. A double variable sum is also declared and initialized with an initial value of 0. It is used to accumulate the sum of Double elements in an array.
Then the program starts a for-each loop that iterates over each Double object in numArray. For each iteration, the doubleValue() method is called on the Double object. This built-in Java method converts a Double object to a primitive double data type. Then add the double value to the sum.
Once all elements in numArray have been processed, use System.out.println(sum); to print the final value of sum to the console. Therefore, this Java code shows how to use the doubleValue() method when working with an array of Double objects, highlighting its usefulness in aggregation operations such as calculating the sum of elements.
The doubleValue() method can also be used in collections such as lists or collections of Double objects.
import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<Double> numList = Arrays.asList(new Double(1.1), new Double(2.2), new Double(3.3)); double product = 1; for (Double num : numList) { product *= num.doubleValue(); } System.out.println(product); } }
7.986000000000001
This Java program, contained in a class named "Main", demonstrates the use of the doubleValue() method when dealing with a list of Double objects.
The entry point of the program is the main method. Here, a list of Double objects, numList, is declared and initialized with three elements: 1.1, 2.2, and 3.3. A double variable product is also declared and initialized to 1. This variable will store the running product of the elements in the List.
Then the program executes a for-each loop to traverse each Double object in numList. During each iteration, the doubleValue() method is called on the current Double object. This method is built into the Double class in Java and converts a Double object to a primitive double data type. The resulting double value is then multiplied with the product, updating its value.
After completing the loop and processing all elements in numList, print the final product to the console through the System.out.println(product); statement. Therefore, this program effectively demonstrates the use of the doubleValue() method when dealing with a collection of Double objects, specifically for cumulative multiplication operations.
The double long Value() method in Java provides a simple and efficient way to convert a Double object into a primitive double, proving its usefulness in various scenarios. Whether you are using basic conversions, mathematical operations, arrays, or collections, the doubleValue() method enables seamless conversions that enhance your Java programming experience.
The above is the detailed content of Using example double longValue() function in Java. For more information, please follow other related articles on the PHP Chinese website!