TwoA
TwoA

Reputation: 85

Isn't main(String args[]) a dynamic array?

I know that in public static void main(String args[]) args is an array that will store command line arguments. But since command line arguments are passed during runtime, is the array args[] a dynamic array? In Java we know that an ArrayList is used to accomplish that kind of a job, so how does a simple array object store those arguments at runtime?

Upvotes: 4

Views: 412

Answers (4)

Elliott Frisch
Elliott Frisch

Reputation: 201439

Every array passed to every function is dynamic in the sense that the array is dynamic from the callee's prospective. As for the special case of main; there is a mechanism called globbing that the shell (or command processor) of the operating system runs to then invoke the Main function (sometimes also called an Entry point). But this is a function of the Operating System (and the JVM) itself.

Upvotes: 1

user395760
user395760

Reputation:

Every array's size is determined at run time. The part that's not dynamic is that an array can't change its size after it is created, and that's also true of the array passed to main.

Upvotes: 3

Steve
Steve

Reputation: 7271

Java arrays can have their size defined at runtime, not just compile time (unlike C stack allocated arrays). However, the size of an array cannot be changed once it has been created.

It is perfectly valid to have an array created at runtime. It is not possible to change the size after it has been created though:

    int argCount = 5;
    // ...
    String test[] = new String[argCount];

An ArrayList lets you grow and shrink the size of the underlying list at runtime.

Upvotes: 3

isnot2bad
isnot2bad

Reputation: 24444

Why do you think, the args array has to be dynamic? The java virtual machine simply invokes the main method and passes the command line arguments as String array. There is no more 'magic' behind this!

Upvotes: 0

Related Questions