4

我知道 inpublic static void main(String args[]) args是一个将存储命令行参数的数组。但是由于命令行参数是在运行时传递的,数组args[]是动态数组吗?在 Java 中,我们知道 ArrayList 用于完成这种工作,那么简单的数组对象如何在运行时存储这些参数?

4

4 回答 4

3

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.

于 2013-11-09T16:54:10.647 回答
3

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.

于 2013-11-09T16:54:36.240 回答
1

传递给每个函数的每个数组都是动态的,因为从被调用者的角度来看,数组是动态的。至于 main 的特殊情况;有一种称为 globbing 的机制,操作系统的 shell(或命令处理器)运行它然后调用 Main 函数(有时也称为入口点)。但这是操作系统(和 JVM)本身的功能。

于 2013-11-09T16:58:49.773 回答
0

为什么你认为 args 数组必须是动态的?Java 虚拟机简单地调用 main 方法并将命令行参数作为字符串数组传递。这背后没有更多的“魔法”!

于 2013-11-09T16:54:03.507 回答