YouTip LogoYouTip

Method Varargs1

# Java Example - Using Varargs in Overloaded Methods [![Image 3: Java Examples](#) Java Examples](#) The following example demonstrates how to use varargs in overloaded methods: ## Main.java File public class Main{static void vaTest(int ... no){System.out.print("vaTest(int ...): " + "Number of parameters: " + no.length +" Content: "); for(int n : no)System.out.print(n + ""); System.out.println(); }static void vaTest(boolean ... bl){System.out.print("vaTest(boolean ...) " + "Number of parameters: " + bl.length + " Content: "); for(boolean b : bl)System.out.print(b + ""); System.out.println(); }static void vaTest(String msg, int ... no){System.out.print("vaTest(String, int ...): " + msg +"Number of parameters: "+ no.length +" Content: "); for(int n : no)System.out.print(n + ""); System.out.println(); }public static void main(String args[]){vaTest(1, 2, 3); vaTest("Test: ", 10, 20); vaTest(true, false, false); }} The output of the above code is: vaTest(int ...): Number of parameters: 3 Content: 1 2 3 vaTest(String, int ...): Test: Number of parameters: 2 Content: 10 20 vaTest(boolean ...) Number of parameters: 3 Content: true false false [![Image 4: Java Examples](#) Java Examples](#)
← Collection CompareData_Linklist β†’