why psvm(String[] args) and not psvm(String ... args)? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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?

12th Sep 2022, 4:27 AM
Harsha S
Harsha S - avatar
5 ответов
+ 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
12th Sep 2022, 9:31 AM
A͢J
A͢J - avatar
+ 3
You can check length of args
12th Sep 2022, 7:25 AM
A͢J
A͢J - avatar
+ 3
Yes. arg.length will tell how many..! And both ways works fine and same.
12th Sep 2022, 8:18 AM
Jayakrishna 🇮🇳
+ 2
A͢J thank you
12th Sep 2022, 11:47 AM
Harsha S
Harsha S - avatar
+ 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; } }
12th Sep 2022, 8:55 AM
Harsha S
Harsha S - avatar