Why does the NetBeans precompiler give a warning for this?
public class PrimitiveVarArgs { public static void main(String[] args) { int[] ints = new int[]{1, 2, 3, 4, 5}; prints(ints); } static void prints(int... ints) { for(int i : ints) System.out.println(i); } }
He complains about line 5, saying:
Confusing primitive array passed to vararg method
but as far as I ( and others on SO ) know, int... matches int[] . This works if it is not a primitive type, such as String , but not for primitives.
I canโt even add this method:
void prints(int[] ints) { for(int i : ints) System.out.println(i); }
because the compiler says:
name clash: prints(int[]) and prints(int...) have the same erasure cannot declare both prints(int[]) and prints(int...) in PrimitiveVarArgs
After examining the NetBeans tooltip settings, I find that this says the following:
The simplest array passed to the variable-argument method will not be unpacked, and its elements will not be considered as variable-length argument elements in the called method. Instead, the array will be passed as a separate element.
However, when I run the file (since this is just a warning, not an error), I get exactly the expected result:
1 2 3 4 5
So why do I like NetBeans to pass my own array to the varargs method?