Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Lambda

Method References

Method references in Java provide a way to refer to methods or constructors without invoking them. They are often more concise and readable than lambda expressions, especially when the lambda expression merely calls an existing method. Method references are a shorthand notation of lambda expressions.

Static Method References:

Static method references refer to static methods using the syntax ClassName::staticMethodName.

Example:

import java.util.Arrays;
import java.util.List;

public class StaticMethodReferenceExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

        // Using static method reference
        names.forEach(System.out::println);
    }
}

In the above example, System.out::println is a static method reference to the println method of the System class.

Instance Method References:

Instance method references refer to instance methods of a particular object using the syntax object::instanceMethodName.

Example:

import java.util.function.Predicate;

public class InstanceMethodReferenceExample {
    public static void main(String[] args) {
        Predicate<String> isNotEmpty = String::isEmpty;

        System.out.println(isNotEmpty.test("Hello")); // Output: true
        System.out.println(isNotEmpty.test(""));      // Output: false
    }
}

In the above example, String::isEmpty is an instance method reference for the isEmpty method of the String class.

Constructor References:

Constructor references create new objects using constructors. They follow the syntax ClassName::new.

Example:

import java.util.function.Supplier;

class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class ConstructorReferenceExample {
    public static void main(String[] args) {
        Supplier<Person> personSupplier = Person::new;

        Person person = personSupplier.get();
        person.setName("Alice");

        System.out.println(person.getName()); // Output: Alice
    }
}

In the above example, Person::new is a constructor reference that creates new Person objects.

Method references enhance the readability of your code by allowing you to express instances where a lambda expression simply calls an existing method. They provide a clear and concise way to represent functional interfaces, making your code more expressive and easier to understand.

PreviousLambda Expressions in CollectionsNextFunctional Programming Concepts

Last updated 1 year ago