Skip to main content

Posts

Showing posts from October, 2021

What does :: mean in Java?

The :: operator refers to a method reference.   A method reference is a simplified way of writing a lambda expression in order to call a method. Method references allow you to call a method by mentioning its name. The syntax for a method reference is as follows: COPY Object :: methodName There are  four  ways to use a method reference: A method reference to a static method. A method reference to an instance method of an object. A method reference to instance methods of an arbitrary object of a particular type. A method reference to a constructor. METHOD REFERENCE TO A STATIC METHOD Let's take a look at the code snippet below: COPY import java.util.function.BiFunction; class Maths { public static int printAddition( int x, int y){ return x + y; } } public class Main { public static void main( String [] args) { BiFunction< Integer , Integer , Integer > addition = Maths::printAddition; int result = addition.apply( 2 , 4 );