### Java Double valueOf() example

In this guide, you will learn about the Double valueOf() method in Java programming and how to use it with an example.

## 1. Double valueOf() Method Overview

### Definition:

The valueOf() method of the Java Double class returns a Double instance representing the specified double value or a String containing a double value.

### Syntax:

``````1. Double Double.valueOf(double d)
2. Double Double.valueOf(String s) throws NumberFormatException``````

### Parameters:

- double d: The value to be represented as a Double instance.

- String s: The string to be converted into a Double instance.

### Key Points:

- It provides an optimized, cache-driven method to retrieve a Double instance (potentially reusing previously cached values for specific number ranges).

- The string-based variant of the method can throw a NumberFormatException if the provided string is not a parsable double.

- Useful for converting both primitive double values and string representations of double values into Double objects.

## 2. Double valueOf() Method Example

``````public class ValueOfExample {
public static void main(String[] args) {
// Using valueOf with double primitive
double primitiveValue = 123.45;
Double doubleObject1 = Double.valueOf(primitiveValue);
System.out.println("Double object from primitive: " + doubleObject1);

// Using valueOf with string representation
String stringValue = "456.78";
Double doubleObject2 = Double.valueOf(stringValue);
System.out.println("Double object from string: " + doubleObject2);

// Exception case (uncomment to see the error)
// String invalidString = "not a number";
// Double doubleObject3 = Double.valueOf(invalidString);  // Throws NumberFormatException
}
}
``````

### Output:

```Double object from primitive: 123.45
Double object from string: 456.78
```

### Explanation:

In the example provided, we first use the valueOf() method to convert a primitive double value, 123.45, into its Double object representation.

Subsequently, the string representation of a double value, "456.78", is converted into a Double object.

A commented-out portion demonstrates the exception that arises when attempting to convert an invalid string into a Double object.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5979539,"math_prob":0.5362074,"size":2153,"snap":"2023-40-2023-50","text_gpt3_token_len":442,"char_repetition_ratio":0.20009306,"word_repetition_ratio":0.0,"special_character_ratio":0.21969345,"punctuation_ratio":0.15745856,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9835095,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-11-29T06:17:59Z\",\"WARC-Record-ID\":\"<urn:uuid:7e09aedc-2666-4228-a1bf-aaa48b431156>\",\"Content-Length\":\"174174\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:04ad390d-51bf-40e8-8362-594cebec89ef>\",\"WARC-Concurrent-To\":\"<urn:uuid:04af6617-a8a7-4019-bda6-d05dd4981e28>\",\"WARC-IP-Address\":\"172.253.122.121\",\"WARC-Target-URI\":\"https://www.javaguides.net/2023/09/java-double-valueof-example.html\",\"WARC-Payload-Digest\":\"sha1:272Q5QPKSPB6HSWJZTOQDPSRPJIA2NHZ\",\"WARC-Block-Digest\":\"sha1:LGZ63DWVNNYK4AQ7B4YG3QZKYBZXEBCR\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100056.38_warc_CC-MAIN-20231129041834-20231129071834-00307.warc.gz\"}"}