Grokking Java Interview Questions
0% completed
Optional
What should the method findPrice("peach") return when the store has no price for a peach? The number 0 would make the product cost nothing. An exception suits an error, but a missing price is an expected answer here. The value null is possible, and the method signature gives no sign of it.
Java 8 added a type for this case. The four questions show what the type changes for the caller, which method supplies a default value, how several lookups form one chain, and in which places the type causes problems.
What problem does Optional solve?
Level: Junior
**`Optional<T>
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%