Core Java · Chapter 34 of 48
Optional
By the end of this chapter you can write a lookup that says "maybe nothing" in its return type, and chain, unwrap or fail on that answer without a single null check. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Core Java
- 6 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
Ravi forwards a crash report from the CSMT counter. A new boy typed "Kulfi" into the till, the price lookup returned null, and three lines later price * qty threw a NullPointerException in the 6 am rush. He wants the till to say "not on the menu", not die.
With what you know so far, a lookup that finds nothing returns null or a made-up price like -1, and every caller must remember to check. Miss one check and you get the crash, or a bill of minus forty rupees. Nothing in the method's type warns the caller.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Lookup.java: look up a price as an Optional, not null
In Lookup.java, make findPrice(item) return an Optional holding the item's price from the menu, or an empty Optional when the item is not on the menu. It never returns null.
For example, findPrice("Masala chai") returns Optional[40], findPrice("Bun maska") returns Optional[50], and findPrice("Kulfi") returns Optional.empty.
- Task 2
Lookup.java: a menu line, or a fallback for a missing item
In Lookup.java, make priceLine(item) return the item, a colon, a space, the rupee sign and the price for an item on the menu, and the item followed by ": not on the menu" for one that is not.
For example, priceLine("Masala chai") returns "Masala chai: ₹40", priceLine("Cutting chai") returns "Cutting chai: ₹25", and priceLine("Kulfi") returns "Kulfi: not on the menu".
- Task 3
Customer.java and Lookup.java: chain the customer and UPI lookups
First, in Customer.java, make upiId() return the upi field as an Optional, empty when the field is null. Then, in Lookup.java, make upiOf(phone) return the UPI id of the regular with that phone number, but only when the id contains "@". It is empty when there is no customer with that phone, when the customer has no UPI id, or when the id has no "@".
For example, upiOf("9845012345") returns Optional[asha@upi]; Imran (9820098200) never gave one and Kiran (9900990099) gave kiran-at-okaxis, so both return Optional.empty, and so does 9000000000, which belongs to no regular.
- Task 4
Lookup.java: throw a clear exception for a missing item
In Lookup.java, make requirePrice(item) return the item's price as an int, and throw an IllegalArgumentException with the message "Not on the menu: " followed by the item when it is not on the menu.
For example, requirePrice("Bun maska") returns 50, and requirePrice("Kulfi") throws an IllegalArgumentException with the message Not on the menu: Kulfi.
- Task 5
Lookup.java: greet a regular by name or welcome a new customer
In Lookup.java, make greet(phone) print exactly one line: "Welcome back, " and the customer's name for a regular with that phone number, or "Welcome to Tapri" for anyone else. Each line ends with a line break, and nothing else is printed.
For example, greet("9845012345") prints Welcome back, Asha; greet("9820098200") prints Welcome back, Imran; greet("9000000000") prints Welcome to Tapri.
- Task 6 · interview question
Interview: Lookup.java, predict when orElse and orElseGet run their fallback
In Lookup.java, make predictOutput() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them.
The check runs the same code and compares your list with what Java really printed.
What you will learn
- Optional: a box that holds one value or nothing, made with of, empty or ofNullable.
- map, filter and flatMap: work on the value inside, if there is one.
- Getting it out: orElse, orElseGet, orElseThrow and ifPresentOrElse, and when each fallback runs.
- Where Optional does not belong: fields and parameters.
More in Core Java
Build "Optional" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.