Understanding the Double Colon in Java

Understanding the Double Colon in Java

Iterating a list and printing the values is very simple, as illustrated by the code snippet below.

import java.util.*;public class DoubleColon{   public static void main(String args[])   {      DoubleColon doubleColon = new DoubleColon();      doubleColon.proceed();   }      private void proceed()   {      List oddNumbers = new ArrayList();      //List oddNumbers is created and populated with some odd numbers      oddNumbers.add(1);      oddNumbers.add(3);      oddNumbers.add(5);      oddNumbers.add(7);      oddNumbers.add(9);      System.out.println("Contents of the oddNumbers list");      //Using the :: here. This explicitly understands and prints the items in the list      oddNumbers.forEach(System.out::println);      }}/*

Expected output:

[root@mypc]# java DoubleColonContents of the oddNumbers list13579*/

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist