0
why psvm(String[] args) and not psvm(String ... args)?
how does public static void main(String[] args) know how many arguments were provided in the command line?
5 odpowiedzi
+ 3
Harsha S
We provide command line arguments with space so when application runs JVM internally converts arguments to a String array which is pass to main method.
If you define method with int array then you have to pass array object you cannot directly pass values like that
+ 3
You can check length of args
+ 3
Yes. arg.length will tell how many..!
And both ways works fine and same.
+ 2
A͢J
thank you
+ 1
A͢J
then why is it not possible to create a method which has variable arguments using int[] args or any array?
public class Program
{
public static void main(String[] args) {
System.out.println(add(5,4,5,3));
}
static int add(int[] arr) {
int sum = 0;
for(int i: arr) {
sum +=i;
}
return sum;
}
}