Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

arrays

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?

like image 655
TwoA Avatar asked Mar 21 '23 17:03

TwoA


1 Answers

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.

like image 113
Steve Avatar answered Mar 24 '23 09:03

Steve