• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with method signatures  RSS feed

 
Laimonas Oberauskis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have the following code:



The compiler says "The method test(Integer[]) is ambiguous for the type Oak". Why can't Java just use the method that takes int... parameters since 5 is simply an int?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the answer has to do with autoboxing.  As you may know, Integer is a wrapper around int, so that you can use int as an object.  Putting an int into an Integer is called "boxing."  Java will do a lot of boxing and unboxing for you automatically.

So I suspect that Java doesn't know if you want to box your int into an Integer or not.

Search on autoboxing and method signatures.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That shoul‍d be described in the Java® Language Specification (=JLS). Beware: that section is probably even harder to understand than the rest of the JLS. It quotes this section which suggests boxing/unboxing (which is what you have here) is assessed before variable arity is assessed. In which case those two declarations are override‑equivalent.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!