Sobes.tech
Junior — Middle

What will happen if you pass a null value to the static method Optional.of?

sobes.tech AI

Answer from AI

If a null value is passed to the static method Optional.of, a NullPointerException will be thrown. The Optional.of method is intended to create an Optional object containing a non-empty value and does not allow null. If there is a possibility that the value could be null, the Optional.ofNullable method should be used, which correctly handles null and creates an empty Optional.