Java Method References

In Java, a method reference is a shorthand syntax for referring to an existing method by its name. It provides a way to pass a method as an argument to a function, without actually invoking the method.

There are four types of method references in Java:

  1. Reference to a static method: This type of method reference is used to reference a static method of a class. For example, if there is a static method myMethod() in a class MyClass, we can refer to it using the following syntax: MyClass::myMethod.
  2. Reference to an instance method of an object: This type of method reference is used to reference an instance method of an object. For example, if there is an instance method myMethod() in a class MyClass, we can refer to it using the following syntax: myObject::myMethod, where myObject is an instance of MyClass.
  3. Reference to an instance method of a particular object: This type of method reference is used to reference an instance method of a specific object. For example, if there is an instance method myMethod() in a class MyClass, and we have an instance of MyClass called myObject, we can refer to it using the following syntax: myObject::myMethod.
  4. Reference to a constructor: This type of method reference is used to reference a constructor of a class. For example, if there is a constructor MyClass() in a class MyClass, we can refer to it using the following syntax: MyClass::new.

Method references can be used in conjunction with functional interfaces to provide a more concise syntax for lambda expressions. They can also improve the readability of code by making it more declarative and expressive.

Types of Method References:

In Java, there are four types of method references that can be used:

  1. Reference to a static method: A reference to a static method is used when you want to refer to a static method of a class. The syntax for this type of method reference is ClassName::methodName. For example, Integer::parseInt is a reference to the static method parseInt of the Integer class.
  2. Reference to an instance method of an object: A reference to an instance method of an object is used when you want to refer to an instance method of an object. The syntax for this type of method reference is objectName::methodName. For example, str::toUpperCase is a reference to the instance method toUpperCase of the String object str.
  3. Reference to an instance method of a particular object: A reference to an instance method of a particular object is used when you want to refer to an instance method of a specific object. The syntax for this type of method reference is ClassName::methodName. For example, System.out::println is a reference to the instance method println of the PrintStream object out of the System class.
  4. Reference to a constructor: A reference to a constructor is used when you want to refer to a constructor of a class. The syntax for this type of method reference is ClassName::new. For example, ArrayList::new is a reference to the constructor of the ArrayList class.

Method references can be used in functional interfaces to provide a more concise syntax for lambda expressions. They can also improve the readability of code by making it more declarative and expressive.