Java Method References — Free Java Tutorial

Learn Java Method References in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

Java Method References — Free Java Tutorial

Learn Java Method References in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

✓ 100% Free ✓ No Login Needed ✓ NCERT / CBSE Aligned ✓ Download as PDF

TL;DR: Learn Java Method References in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.

Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated

🤖 Stuck on any question? Ask Syllab's free AI Tutor for a step-by-step explanation — instant, unlimited, no login.

Java Method References in Java

A method reference is a shorthand for a lambda expression that calls a single existing method. Syntax: ClassName::methodName or instance::methodName.

Four types: (1) Static method: ClassName::staticMethod, (2) Instance method of a specific object: obj::method, (3) Instance method of an arbitrary instance: ClassName::instanceMethod, (4) Constructor reference: ClassName::new.

Method references improve readability when a lambda does nothing but call a method: s -> s.toUpperCase() becomes String::toUpperCase.

They work wherever a functional interface is expected — any context that accepts a lambda.

Java Method References — Syntax

// Lambda vs Method Reference
list.forEach(s -> System.out.println(s));  // lambda
list.forEach(System.out::println);          // method ref

list.stream().map(s -> s.toUpperCase())    // lambda
list.stream().map(String::toUpperCase)      // method ref (instance method)

Arrays.sort(arr, (a,b) -> Integer.compare(a,b));  // lambda
Arrays.sort(arr, Integer::compare);                // method ref (static)

// Constructor reference
List<String> list = Stream.of("a","b").collect(Collectors.toCollection(ArrayList::new));

Learn Java Method References step by step with Syllab's free interactive Java tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Java course →