Core Java · Chapter 31 of 48
Method references
By the end of this chapter you can replace a lambda that only calls one method with a method reference, and tell the four kinds apart. 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
Kabir is reviewing last week's lambda code, and half the lambdas look like text -> text.strip(): a parameter, an arrow, and one call to a method that already exists. He wants them shorter, and he wants the code to say which method runs, not how the parameter is passed along.
Anjali has a ticket that fits: names typed at the counter come with stray spaces and blank lines, and she wants a clean, sorted list for the loyalty cards. With lambdas alone, each step is one more x -> x.something(), and the names of the methods get lost in the arrows.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Refs.java: point at static methods to parse and compare prices
In Refs.java, make toPrice() return a method reference to a static method that turns the text of a whole number into that number; text that is not a number throws a NumberFormatException. Make larger() return a method reference to a static method that gives the larger of two numbers.
For example, toPrice gives 40 for "40" and 125 for "125", and throws a NumberFormatException for "forty". larger gives 40 for 25 and 40, and 50 for 50 and 40.
- Task 2
Refs.java: point at a method of one particular list
In Refs.java, make onMenu(menu) return a predicate, as a method reference bound to the menu list, that is true when an item is on that list, including items added to the same list later. Make logTo(log) return a consumer, as a method reference bound to the log list, that adds each message to the end of log.
For example, with a menu of Masala chai and Cutting chai, onMenu is true for Masala chai and false for Bun maska; after Bun maska is added to the menu, it is true for Bun maska. Accepting SBC opened and then first train leaves log as SBC opened, first train.
- Task 3
Refs.java: point at a String method for whatever string comes in
In Refs.java, make shout() return an unbound method reference that gives the string it is applied to in capitals. Make sameName() return an unbound method reference that is true when two names are equal, ignoring upper and lower case.
For example, shout gives SBC for sbc and MASALA CHAI for Masala chai. sameName is true for Ravi and RAVI, and for anjali and Anjali, and false for Meera and Mira.
- Task 4
Refs.java: point at constructors to make tickets, lists and trays
In Refs.java, make newTicket() return a constructor reference that builds a new Ticket for a station code. Make freshList() return a constructor reference that gives a new, empty, changeable list on every call, never the same list twice. Make tray() return an array constructor reference that builds a new int array of the given length, 0 included.
For example, newTicket gives a Ticket equal to new Ticket for SBC, and one whose station is NDLS for NDLS. Two calls of freshList give two different lists; adding Masala chai to one leaves the other empty. tray gives an array of length 6 for 6, and of length 0 for 0.
- Task 5
Refs.java: tidy a list of names with method references
In Refs.java, make tidy(names) return a new list: every name with the spaces at both ends removed, blank names dropped (empty, or only spaces), sorted A to Z ignoring upper and lower case. The given list is not changed (it may be unmodifiable). A list of only blank names gives an empty list.
For example, " ravi ", "", "Anjali", " ", "meera" and "Kabir " give Anjali, Kabir, meera, ravi, and the given list still has 6 names; " " and "" give an empty list.
- Task 6 · interview question
Interview: Refs.java, predict when a bound reference reads its object
In Refs.java, make predictReceiver() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (a number like 7).
The check runs the same code and compares your list with what Java really printed.
What you will learn
- Static method references: Type::staticMethod, the arguments go to the method.
- Bound references: object::method, a method of one particular object.
- Unbound references: Type::instanceMethod, the first argument becomes the object the method runs on.
- Constructor references: Type::new and int[]::new.
More in Core Java
Build "Method references" 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.