devxlogo

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*/

devx-admin

Share the Post: