What are variadic functions in Java?

Java 8Object Oriented ProgrammingProgramming

Methods which uses variable arguments (varargs, arguments with three dots) are known as variadic functions.

Example

Live Demo

public class Sample {
    void demoMethod(String... args) {
      for (String arg: args) {
         System.out.println(arg);
      }
    }

   public static void main(String args[] ){
      new Sample().demoMethod("ram", "rahim", "robert");
      new Sample().demoMethod("krishna", "kasyap");
   }
}

Output

ram
rahim
robert
krishna
kasyap
raja
Published on 02-Jan-2018 07:27:58
Advertisements