• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable arguments

 
Greenhorn
Posts: 24
Redhat Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The 11th line is compiling error, but the 10th line is OK, why ? How does the compiler understanding them ? Thank you !
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what is the compile time error? Tell us the details - it makes life much easier for all of us.
 
Song Guo
Greenhorn
Posts: 24
Redhat Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:And what is the compile time error? Tell us the details - it makes life much easier for all of us.



The following Screenshot:
Screenshot-21.png
[Thumbnail for Screenshot-21.png]
compile error
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, you get that error when there are two different methods that match the call, and the compiler has no rule to determine the priority.

The key point here are:
- 1 is an int. This can automatically be converted to a float. It can't be autoboxed to a Character.
- 'a' is a char, which is an integer type. Which means it can be autoboxed to a Character, but it can also be converted to a float.

This means that line 10 can match test(float, Character...), but it can't match test(Character...), because 1 can't match Character. So there's no ambiguity.

However, line 11 can match both. At this point the compiler checks its rules for prioritising. These can get a bit complicated (read the Java Language Specification for the full rules), but it turns out that none of them can be applied in this case. So the compiler doesn't know what to do, and you get the error.
 
Song Guo
Greenhorn
Posts: 24
Redhat Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:OK, you get that error when there are two different methods that match the call, and the compiler has no rule to determine the priority.

The key point here are:
- 1 is an int. This can automatically be converted to a float. It can't be autoboxed to a Character.
- 'a' is a char, which is an integer type. Which means it can be autoboxed to a Character, but it can also be converted to a float.

This means that line 10 can match test(float, Character...), but it can't match test(Character...), because 1 can't match Character. So there's no ambiguity.

However, line 11 can match both. At this point the compiler checks its rules for prioritising. These can get a bit complicated (read the Java Language Specification for the full rules), but it turns out that none of them can be applied in this case. So the compiler doesn't know what to do, and you get the error.



Thank you very much, I understand.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic