6

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);
    }
}

It complains about line 5, saying:

Confusing primitive array passed to vararg method

but as far as I (and others on SO) know, int... is the same as int[]. This works if it's a non-primitive type, like String, but not on 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

Upon looking into the NetBeans hints settings, I find it says this:

A primitive array passed to variable-argument method will not be unwrapped and its items will not be seen as items of the variable-length argument in the called method. Instead, the array will be passed as a single item.

However, when I run the file (since it's just a warning and not an error), I get exactly the output I expect:

1
2
3
4
5

so, why doesn't NetBeans like me passing a native array to a varargs method?

Community
  • 1
  • 1
Ky -
  • 30,724
  • 51
  • 192
  • 308
  • @SotiriosDelimanolis: Funny thing is, both of these questions are on the front page right now – Engineer2021 Aug 20 '14 at 15:48
  • 1
    The question does not deserve the downvotes. But you should rephrase the question so that it doesn't look exactly like the other. – Sotirios Delimanolis Aug 20 '14 at 15:51
  • 1
    @staticx The question isn't the same, though. OP just wants the answer, which is that this is a Netbeans bug, moved here. – Sotirios Delimanolis Aug 20 '14 at 15:51
  • @SotiriosDelimanolis: As it is phrased and laid out right now, it looks like an exact dupe. – Engineer2021 Aug 20 '14 at 15:53
  • 1
    Have you tried updating NetBeans? [This link](https://netbeans.org/bugzilla/show_bug.cgi?id=242627) seems to indicate that the problem has been fixed, although I can't tell whether the fix is available yet. – ajb Aug 20 '14 at 15:56
  • @SotiriosDelimanolis the upvote or downvote is a matter of what other users think if this question is useful (or not) for future readers, so I don't think there's a good/bad reason for voting this specific question. Apart of that, this bug can be easily searched on the net, no need to raise a question here... – Luiggi Mendoza Aug 20 '14 at 15:58
  • 2
    OH MY GOSH GUYS you ask me to move it to a new question and then you mark it as a duplicate?! – Ky - Aug 20 '14 at 20:15
  • 1
    Note: "guys" is probably incorrect, note only one user marked this question as a duplicate. I'd consider opening a meta discussion in your shoes. – jdphenix Aug 20 '14 at 20:42
  • 1
    @LuiggiMendoza so an SO question is invalid because there's something on the Internet that answers it? Don't see any reference to such a concept in the rules. – jdphenix Aug 20 '14 at 20:45
  • @jdphenix doesn't it take a consensus of 5 to close a question? – Ky - Aug 20 '14 at 23:26
  • @Supuhstar http://chat.stackoverflow.com/rooms/59693/room-for-jdphenix-and-supuhstar – jdphenix Aug 20 '14 at 23:43
  • 1
    @Supuhstar: People with gold badges in a tag can close questions with 1 vote. – Engineer2021 Aug 25 '14 at 19:53

1 Answers1

6

It's a bug in NetBeans.

https://netbeans.org/bugzilla/show_bug.cgi?id=242627

Consider the following code:

public class Test {
    public static void main(String[] args) {
        int[] ints = new int[]{1, 2, 3, 4, 5};
        prints(ints);
    }

    static void prints(Object... ints) {
        for(Object i : ints) {
            System.out.println(i);
        }
    }
}

Output:

[I@15db9742 // on my machine

Versus this code:

public class Test {
    public static void main(String[] args) {
        Integer[] ints = new Integer[]{1, 2, 3, 4, 5};
        prints(ints);
    }

    static void prints(Object... ints) {
        for(Object i : ints) {
            System.out.println(i);
        }
    }
}

Output:

1
2
3
4
5

Notice in my examples the signature of prints(). It accepts Object..., not int.... NetBeans is trying to warn you that something "strange" (unexpected) might happen, but erroneously reports that prints(int...) could do something "unexpected".

jdphenix
  • 15,022
  • 3
  • 41
  • 74
  • I'm using NetBeans 8.0. I note that bug is marked as `RESOLVED FIXED`, but do you know when that'll propagate to stable releases? – Ky - Aug 20 '14 at 23:30
  • 1
    @Supuhstar No, but I'd just disable it for now (hover, click on `Configure confusing varargs...`, uncheck, go from there) – jdphenix Aug 20 '14 at 23:58
  • I'm using Netbeans 11.2, and I confirm the bug is still there. – donnadulcinea Apr 25 '20 at 05:35