Wednesday, July 3, 2024
HomeLanguagesJavaOptionalInt of(int) method in Java with examples

OptionalInt of(int) method in Java with examples

The of(int) method help us to get an OptionalInt object which contains a int value which is passed as a parameter to this method.

Syntax:

public static OptionalInt of(int value)

Parameters: This method accepts a int value as parameter that will be set to the returned OptionalInt object.

Return value: This method returns an OptionalInt with the value present.

Below programs illustrate of(int) method:
Program 1:




// Java program to demonstrate
// OptionalInt.of(int) method
  
import java.util.OptionalInt;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a OptionalInt instance
        // using of() method
        OptionalInt opInt
            = OptionalInt.of(452);
  
        // Get value of this OptionalInt instance
        System.out.println("OptionalInt: "
                           + opInt);
    }
}


Output:

OptionalInt: OptionalInt[452]

Program 2:




// Java program to demonstrate
// OptionalInt.of(int) method
  
import java.util.OptionalInt;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a OptionalInt instance
        // using of() method
        OptionalInt opInt
            = OptionalInt.of(2149);
  
        // Get value of this OptionalInt instance
        System.out.println("OptionalInt: "
                           + opInt);
    }
}


Output:

OptionalInt: OptionalInt[2149]

References: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalInt.html#of(int)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments