| by Arround The Web | No comments

What Does Double Colon (::) Mean in Java

In Java, many operators are used to perform different types of tasks, one of them is a “::” (double colon) called “Method Reference” operator. This operator is a replacement of Lambda Expression and is used to refer to the method, whether it is a static method, constructor, or an instance method.

This tutorial will illustrate what does “::” mean and how to use it in Java.

What Does “::” Mean in Java?

Double colon “::” is a method reference operator used to call the method. It refers to the specified method with the help of the class name. This operator behaves similar to the Lambda Expression. However, it does not need method arguments for references. That’s why “::” is simple and more effective.

Now, let’s examine the below example to check the basic working of the mentioned operator.

Example
First, we will create a String type list for numbers named “num”:

List<String> num= new ArrayList<String>();

Then, add the required elements in the created list using the “add()” method:

num.add("one");
num.add("Two");
num.add("Three");
num.add("Four");
num.add("Five");

Lastly, utilize the “forEach” loop, and print out the list elements. To do so, we will use the “println()” method of the “System” class and access it using the “::” operator:

num.forEach(System.out::println);

All of the added elements will be printed out on the console:

Now, let’s check out the advanced usage of the “::” operator in Java.

How to Use “::” in Java?

The Method reference operator “::” is used to call different types of methods in Java, such as:

We will now discuss each of the mentioned methods one by one.

Method 1: Call Static Method Using “::” Operator in Java

To call the static method of a class, use the “::” operator as follows:

Syntax

target class name::static method name

In the above-given syntax, mention the target class from where the method is going to be accessed.

Example
We will use the same list created in the above example. However, we will create a static method named “display()” to print the list elements:

static void display(String stng) {
   System.out.println(stng);
}

In the main() method, we will call the static method “display()” of the “Example” class by using the “::” method reference operator:

num.forEach(Example::display);

Output

Want to access a constructor using the method reference operator? Check out the below-given section.

Method 2: Call Constructor Using “::” Operator in Java

You can also utilize the method reference operator to invoke a class constructor.

Syntax

class name::new

Example
In the constructor of the “Example” class, we will call the “System.out.println()” method to print out the added list elements of String type:

public Example(String stng) {
    System.out.println(stng);
}

In the main() method, we call the constructor by using a “new” keyword and adding the “::” operator after specifying the class name. The “new” keyword will create a class object that automatically calls the constructor method:

num.forEach(Example::new);

Output

Let’s head towards the next method!

Method 3: Call Instance Method Using “::” Operator in Java

Similar to the static method, you can also call the instance method of the class by using the “::” operator. For this, first, you will create a class object and then call the instance method with the help of the following syntax.

Syntax

target class object::instance method name

Example
We will now create an object “ex” of the “Example” class:

Example ex= new Example();

Then, call the instance method “display()” using the class object and “::” method reference operator:

num.forEach(ex ::display);

Output

We compiled all the necessary information related to method reference operator “::” and its usage in Java.

Conclusion

The “::” is an operator called “Method Reference Operator” that is utilized to access the method by pointing to it using the class name. This type of Java operator can be utilized to call different types of methods in a program, such as Static methods, Constructors, and Instance methods. This tutorial illustrated what “::” (double colons) means and how to use it in Java.

Share Button

Source: linuxhint.com

Leave a Reply