Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vararg Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this code runs fine


with output:
In Method Id 100 will be printed

i think output should be
Compile time error as it is not clear which method can be called


can anyone tell the reason for this ?
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
var-arg has the lowest precedence. If a program has a choice, it will pretty much always choose anything else over a var-arg. The var-arg is the last resort, because it's least specific. It's like passing a Dog where a method is overloaded to accept either Object or Animal. Object is least specific, so it will choose Animal. Var-args behave the same way. They are the least specific, because if you want, you can pass 100 arguments.

Check out K&B study guide, it clearly states precedence rules. It's in Chapter 3, I believe. Not sure though. But it's in there.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:

i think output should be
Compile time error as it is not clear which method can be called


Why did you think so?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why it should be the compiler error?
perhaps you are thinking static method can't be override but here method is overloaded.
remember varag is a loser in each and every case
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic is a must read about auto-boxing, boxing and var-args.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
above topic will be helpful.

but just remember one thing
compiler choose
widening,
autoboxing and then
var-args.
 
Sven Mathijssen
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arjun srivastava wrote:
why it should be the compiler error?
perhaps you are thinking static method can't be override but here method is overloaded.
remember varag is a loser in each and every case


I understand the confusion, because the call to printName(String, String) is ambiguous in this case. However, the post mentioned by Wouter Oet clearly states the compiler resolving this ambiguity by choosing the declared two-argument method over the varargs variant. In this case it is resolved by precedence of a fixed-number-of-arguments method over a method with varargs.
 
Akshat Kumar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what i know,
String,String seems like a perfect match.

I think the var args method will be chosen if you use an invocation with just 1 String or more than 2 Strings.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

For better understanding have a look at above posts.
 
Gari Jain
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Var-args is the last preference.
(K&B)

read the var-args topic
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic