*
The moose likes Beginning Java and the fly likes Variable arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable arguments" Watch "Variable arguments" New topic
Author

Variable arguments

Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24


The 11th line is compiling error, but the 10th line is OK, why ? How does the compiler understanding them ? Thank you !
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

And what is the compile time error? Tell us the details - it makes life much easier for all of us.
Song Guo
Greenhorn

Joined: Apr 24, 2011
Posts: 24

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:



[Thumbnail for Screenshot-21.png]

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Apr 24, 2011
Posts: 24

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

Joined: Apr 06, 2010
Posts: 4344
    
    8

You're welcome.
 
 
subject: Variable arguments
 
Similar Threads
Using this to reference static variables
how come its compiling??
Float argument
Double and Float size
Override issue