Wednesday, March 1, 2023

Quiz yourself: Use an Optional object when you might have zero data items

Quiz Yourself, Oracle Java, Java Career, Java Tutorial and Materials, Oracle Java Career, Oracle Java Skills, Oracle Java Jobs, Oracle Java Object

Given the following method

public void printSomething(Optional<Integer> o) {
  var p = o.filter(v -> v == null);
  p.ifPresent(System.out::println);
}

and the code fragment

printSomething(Optional.ofNullable(0));    // line 1
printSomething(Optional.ofNullable(null)); // line 2

What is the result? Choose one.

A. There is no output.
B. One blank line is printed.
C. Two blank lines are printed.
D. 0 is printed and then nothing else.
E. 0 is printed followed by a blank line.

Answer. Using a null pointer to indicate that a data item does not exist can be error prone. Another—typically more robust—way to address the possibility of a data item not existing is to use something such as an array. In Java, an array can contain any number of items, and the array knows how many items it contains. If you write code to process all the elements in the array but the array is empty, you simply process nothing, and no consequential errors will occur.

This idea is formalized in the Optional class that was added in Java 8. An Optional object is a kind of wrapper that contains either zero or one data items. The Optional also provides methods that can apply processing to all the elements in the Optional in a manner comparable to what was just outlined for an array, providing another safe way to handle a missing element. This question investigates the behavior of two methods in the Optional API and one of the methods that can create an Optional instance.

The method Optional.ofNullable creates a new Optional object that either contains the provided argument or, if that argument value is null, is empty. So, line 1 in the code snippet above creates an Optional that contains an autoboxed Integer that has a value of zero. Line 2 creates an Optional object representing emptiness.

The Optional.filter() method is similar to the intermediate operation of the Stream method with the same name. It takes a Predicate as an argument and tests elements with that Predicate. The filter method then returns an Optional based on that algorithm, as follows:

◉ If the Optional was empty, filter() returns the same Optional (which is still empty, of course).
◉ If the object contained in the Optional returns false from the Predicate test, an empty Optional is returned.
◉ Otherwise, the same Optional is returned, which still contains the original value.

The filtering operation in the example looks like the following:

var p = o.filter(v -> v == null);

The Predicate shown above returns true only if the argument value is null. In other words, anytime this Predicate is used in a filter operation on an Optional, the result must be an empty Optional. If the original Optional was not empty, the test fails, resulting in an empty result. If the original Optional was actually already empty, the test is never applied and the filter simply returns the original, unchanged—and still empty—Optional.

From the discussion above, you can deduce that both line 1 and line 2 must have the same effect. Option B suggests that one blank line is printed, which could only happen if lines 1 and 2 had different effects. Similarly, options D and E both hinge on different effects for the two lines. You can therefore deduce that options B, D, and E must be incorrect.

Next, consider the behavior of the Optional.ifPresent() method. This method takes a Consumer as its argument. If the Optional contains a value, the Consumer is invoked with the value of the Optional being passed as the argument. If the Optional is empty, ifPresent does nothing.

At this point, you’ve already established that when the code reaches the point of invoking ifPresent, the Optional is empty regardless of the situation prior to that. This in turn shows that no printing is ever invoked. From that, you can determine that the code produces no output and option A is correct, while option C (which suggests that println is called but with an empty string as an argument) is incorrect.

Conclusion. The correct answer is option A.

Source: oracle.com

Related Posts

0 comments:

Post a Comment