You are reading the article Optional Class In Java 8 updated in October 2023 on the website Saigonspaclinic.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Optional Class In Java 8
Introduction to Optional Class in Java 8In java 8(jdk8), java introduces a new class optional class, which is used to void NullPointerException in Java. The NullPointerException can crash the code and it is hard to avoid it without performing many null checks, but by using optional class in a program we can easily check whether a variable contains a null value or not and avoid the NullPointerException. With the help of an Optional class, we can provide alternate code to run or alternate values to return. As the optional class is a built-in class in chúng tôi package.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
{ }
Member Functions of the Optional Class with SyntaxMember functions of the optional class are given below:
1. empty(): This function gives an empty Optional instance(Without any value in it).
2. of(T value): This function gives an Optional instance(with the given non-null value).
3. ofNullable(T value): This function gives an Optional instance(with the given value, if non-null, else an empty Optional.
4. get(): This function gives the optional value if the value is present, else NoSuchElementException throws.
public T get()5. isPresent(): This function gives true if the value is present in an Optional instance, otherwise false.
public boolean isPresent()10. orElse( T other ): This function gives the value, if it is present, otherwise give others.
public T orElse(T other)13. equals( Object ob ): This function returns true if given ob object is equal to this Optional object. Otherwise, return false. The ob object can be equal if it is Optional or both objects have no value present or both objects present equal value.
public boolean equals(Object ob )14. hashCode(): This function gives the hash code if the value is present, else return 0.
public int hashCode()15. toString(): This function gives a string format presentation of an Optional object, this presentation may be different based on the versions and implementations.
public String toString() Examples of Optional Class in JavaNext, we write the java code to understand how null value is present in a variable when we do not initialize the variable with the following example where we without using Optional class to avoid null value, as below –
Example #1Code:
package p1; public class Demo { public static void main( String[] arg) { String[] info = new String[5]; info[0]="John"; info[2]=new Integer(26).toString(); info[3]=info[0].substring(0)+" "+info[1].substring(0); System.out.println("All present values are :"); System.out.println("fname : "+info[0]); System.out.println("lname : "+info[1]); System.out.println("Age : "+info[2]); System.out.println("fullname: "+info[3]); } }Output:
As in the above code, an Optional class is not used, so when we run the above program it throws a nullPointerException and terminates abnormally.
Example #2Code:
package p1; import java.util.Optional; import java.util.Scanner; public class Demo { public static void main( String[] arg) { Scanner sc= new Scanner(System.in); String[] info = new String[5]; info[0]="John"; info[2]=new Integer(26).toString(); for(int i=0; i<2;i++) { if(isNulllname.isPresent()) { info[3]=info[0].substring(0)+" "+info[1].substring(0); System.out.println("All present values are :"); System.out.println("fname : "+info[0]); System.out.println("lname : "+info[1]); System.out.println("Age : "+info[2]); System.out.println("fullname: "+info[3]); break; }else { System.out.println("Last name is not present"); } System.out.println("Enter last name"); info[1]=sc.next(); isNulllname = Optional.ofNullable(info[1]); } } }Output:
An in the above code an Optional class is used to avoid a nullPointerExceptionand the abnormal termination, so the code executes without crashing. And when the value is provided or presents it just printing the values.
Next, we write the java code to use all optional class functions, as below –
Example #3Code:
package p1; import java.util.Optional; public class Demo { public static void main( String[] arg) { String[] info = new String[5]; info[0]="John"; info[2]=new Integer(26).toString(); System.out.print("nempty() = "+e); System.out.print("nofNullable(info[0]) = "+v); System.out.print("nofvalue(info[0]) = "+val); System.out.print("nofNullable(info[1]) = "+v1); System.out.print("nhashCode()= "+val.hashCode()); System.out.print("nget()= "+val.get()); System.out.print("nispresent() = "+val.isPresent()); System.out.print("nval.orElse("Value is not present") = "+val.orElse("Value is not present")); System.out.print("ne.orElse("Value is not present") = "+e.orElse("Value is not present")); System.out.println(val.map(String::toUpperCase)); System.out.println(e.map(String::toUpperCase)); } }Output:
Recommended ArticlesThis is a guide to Optional Class in Java 8. Here we also discuss the function and working of optional class in java 8 along with different examples and its code implementation. You may also have a look at the following articles to learn more –
You're reading Optional Class In Java 8
Update the detailed information about Optional Class In Java 8 on the Saigonspaclinic.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!