I have not been able to reproduce the warning on any of my NetBeans
installations.

   - Windows 10, Java 8, NetBeans 11.2
   - Windows 11, Java 21, NetBeans 28
   - macOS Sonoma, Java 21 (or maybe Java 11), NetBeans 18

I devised a toy example. If I'm understanding the warning correctly, this
function should not do what one would think it does:

    static byte[] gatherFirstElems(byte[]... arrays) {
        int len = arrays.length;
        byte[] bytes = new byte[len];
        for (int i = 0; i < len; i++) {
            bytes[i] = arrays[i][0];
        }
        return bytes;
    }

But it does work as expected, my unit test confirms.

    @Test
    public void testGatherFirstElems() {
        System.out.println("gatherFirstElems");
        byte[] array1 = {1};
        byte[] array2 = {2, 3};
        byte[] array3 = {4, 5, 6};
        byte[] array4 = {7, 8, 9, 10};
        byte[] array5 = {11, 12, 13, 14, 15};
        byte[] expected = {1, 2, 4, 7, 11};
        byte[] actual =
PrimitiveArrayVarArgsExample.gatherFirstElems(array1,
                array2, array3, array4, array5);
        assertEquals(actual, expected); // Use assertArrayEquals() if JUnit
    }

However, I did notice that when I only had array1 as a parameter for
gatherFirstElems(), NetBeans gave a warning: "Confusing primitive array
passed to vararg method".

AL

On Thu, Nov 27, 2025 at 3:35 PM Owen Thomas <[email protected]>
wrote:

> Thanks for your response Alonso.
>
> I needed a method to accept an unspecified number of byte arrays - this
> seemed like a perfect application of the variable arguments notation. It
> just struck me as strange that the editor would give me this warning. I
> don't use the variable arguments notation often (never before where the
> arguments were themselves arrays), so I asked the question in this list in
> the hope that I would understand the rationale that motivated someone to
> have the editor provide the warning.
>
> Thanks again,
>
>   Owen.
>
>

Reply via email to