When using command line arguments in java I am curious to how big is the args array parameter in main. And if it is varied length how does main know the length?
Asked
Active
Viewed 236 times
-1
-
1how many arguments do you pass? – Stultuske Nov 13 '17 at 11:40
-
once it run type s.o.p(args.length) – Basil Battikhi Nov 13 '17 at 11:41
-
Passing a string which is > 2 GB memory ? – Suresh Atta Nov 13 '17 at 11:41
-
"how does main know the length?" — `main` can check the length by examining `args.length`. – khelwood Nov 13 '17 at 11:42
-
Array is a static mem struct, so the length depends on the number of arguments you pass. When args are instantiated in JVM the JVM knows the size. – Francisco Valle Nov 13 '17 at 11:43
-
Francisco that was the answer I was looking for. So will the JVM construct the array before main is called ? I assumed that it acts similar to writing a method that takes in an array of arguments, the programmer will have initialized the size of the array before it is passed to the method. In the case of main, the JVM does this ?? – Anthony Mullen Nov 13 '17 at 20:18
1 Answers
0
It will be as big as needed to hold the arguments passed in. So if you do
java -jar myjar.jar 1 2 34
Args will contain
["1", "2", "34"]
Note the array is typed as string so if you pass in numbers you need to convert the string containing the number to actual number.

chainingsolid
- 13
- 3