Table of Contents
grammar
Glossary explanation
Grammar code
algorithm
method
Method 1: Basic conversion
Example
Output
illustrate
Method 2: Conversion of mathematical operations
Method 3: Array conversion
Method 4: Conversion in Collection
in conclusion
Home Java javaTutorial Using example double longValue() function in Java

Using example double longValue() function in Java

Sep 02, 2023 am 11:29 AM
java Example Double longvalue() function

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.

grammar

The syntax of the Double long Value() method is very simple −

public double doubleValue()
Copy after login

Glossary explanation

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.

Grammar code

Double num = new Double(123.45);
double d = num.doubleValue();
System.out.println(d);
Copy after login

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.

algorithm

  • Initialize the Double object.

  • Call the doubleValue() method on the object.

  • doubleValue() method returns the double value of the object.

method

Let's examine two different ways of using properties in Java Servlets.

Method 1: Basic conversion

In its simplest form, the doubleValue() method is used to convert a Double object to a double primitive.

Example

public class Main {
   public static void main(String[] args) {
      Double num = new Double(789.32);
      double d = num.doubleValue();
      System.out.println(d);
   }
}
Copy after login

Output

789.32
Copy after login

illustrate

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.

Method 2: Conversion of mathematical operations

The doubleValue() method is particularly useful when performing mathematical operations that require double precision.

Example

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);
   }
}
Copy after login

Output

1.4209707529558184
Copy after login

illustrate

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.

Method 3: Array conversion

doubleValue() method can also be used when processing Double object arrays.

Example

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);
   }
}
Copy after login

Output

60.599999999999994
Copy after login

illustrate

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.

Method 4: Conversion in Collection

The doubleValue() method can also be used in collections such as lists or collections of Double objects.

Example

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);
   }
}
Copy after login

Output

7.986000000000001
Copy after login

illustrate

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.

in conclusion

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles