What is the OptionalDouble.of method in Java?

What is the OptionalDouble.of method in Java? Answer

In Java, the OptionalDouble object is a container object that may or may not contain a double value.

The OptionalDouble class is present in the java.util package.

The of method

The of method is used to get an instance of the OptionalDouble class with the specified double value.

Syntax

public static OptionalDouble of (double value)

Parameters

This method takes as a parameter the double value that is to be present in the OptionalDouble object.

Return value

This method returns an OptionalDouble object with the specified double value.

Code

The code below shows how to use the of method.

import java.util.OptionalDouble;
  
class OptionalDoubleOfExample {
    public static void main(String[] args) {
        OptionalDouble optional1 = OptionalDouble.of(1.5);
        System.out.println("Optional 1: " + optional1);

        OptionalDouble optional2 = OptionalDouble.of(10.8967);
        System.out.println("Optional 2: " + optional2);
    }
}

Explanation

In the code above:

  • In line 1, we imported the OptionalDouble class.
import java.util.OptionalDouble;
  • In line 5, we used the of method to get an OptionalDouble object with the double value 1.5.
OptionalDouble optional1 = OptionalDouble.of(1.5);
optional1; // OptionalDouble[1.5]
  • In line 8, we used the of method to get an OptionalDouble object with the value 10.8967.
OptionalDouble optional2 = OptionalDouble.of(10.8967);
optional2;// OptionalDouble[10.8967]
What is the OptionalDouble.of method in Java? Review:

In our experience, we suggest you solve this What is the OptionalDouble.of method in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is the OptionalDouble.of method in Java?

Find on Educative

Conclusion:

I hope this What is the OptionalDouble.of method in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.