Java Optional create from constant

Description

Java Optional create from constant

import java.util.Optional;

public class Main {
  public static void main(String[] args) {
    Optional<String> empty  = Optional.empty();
    System.out.println(empty);/*from   ww w.  j  a  va2 s . c  o m*/

    Optional<String> str = Optional.of("a string value");
    System.out.println(str);

    String a = ""; 
    Optional<String> str2  = Optional.of(a);
    System.out.println(str2);

    a = null; 
    //str2  = Optional.of(a);//throw exception

  }
}



PreviousNext

Related