Skip to main content

Posts

Showing posts with the label Variable Arguments (Varargs) in Java

Variable Arguments (Varargs) in Java

  Variable length arguments (var args) Do Check My Video on Varargs in java: whenever you are not sure how many inputs are going to be provided by the user or you are not sure how many arguments that you need to take inside a method at that time you need to go for variable length arguments void sub(int... arr), ... are like single dimensional array Example: class Test{ public static void m1(int... arr){ System.out.println("You are in the var-args"); } public static void m2(int[] arr){ System.out.println("You are in Array"); }     public static void main(String[] args){ int[] a = {10,20,30}; m1(a); m2(a); } } o/p: You are in the var-args You are in Array Note: in above exaple you can call the method m2 by passing only the array but in m1 method we can call that method with array as well as m1(2,3) so in above example m2(6,7,8) -> Not Allowed but in the m1(3,4,5)->allowed case 1 : Note : JVM will give the list priority to the var- rgs me