Java 可变参数是如何在内存中实现的

How are Java varargs implemented in the memory

据我所知,局部变量和参数存储在堆栈内存中,其中包括对象引用,而实际对象存储在堆内存中。那么当你使用 var-args 时到底发生了什么?

public static int[] test(int... x) {
    return x;
}

public static void main(String[] args) {
    int[] a = test(1,2,3,4,5);
    int[] b = test(6,7,8,9,0);
    System.out.println(a);
    System.out.println(b);
    for (int i : a) {
        System.out.println(i);
    }
    for (int i : b) {
        System.out.println(i);
    }

这里我认为在x中作为参数传递给test的所有值都存储在堆栈中,因此当我们调用test(1,2,3,4,5)时,我们正在用尽堆栈space,相应地,当我们调用 test(6,7,8,9,0) 时,我们应该在堆栈上造成内存冲突......但是当我 运行 以上时,我得到以下结果。

[I@2db0f6b2
[I@3cd1f1c8
1
2
3
4
5
6
7
8
9
0

可以看出,由于第二次调用 test 生成 b,因此 a 中的项目没有损坏。两者的存储方式似乎不同。

这是否意味着参数以某种方式存储在堆中?这是否意味着 function(param) 形式的任何调用都转换为 param 的值(原始或内存引用)不一定位于堆栈内存中?

Varags 只是数组顶部的语法糖 - 即,使用 int... 参数与 int[] 相同。 与所有数组一样,数组驻留在堆上,您将对它的引用传递到堆栈上。

x... 只是 x[] 的语法糖。认为它的工作过程与原始数组完全相同。

The reference itself could be stored on the heap if it is a member of a class or object, or on the stack if it is a local variable in a method. And primitive types can be stored on the heap if they are members of a class or object. - Source