wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Vararg Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Vararg Problem" Watch "Vararg Problem" New topic
Author

Vararg Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



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 ?


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

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.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


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


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

This topic is a must read about auto-boxing, boxing and var-args.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

above topic will be helpful.

but just remember one thing
compiler choose
widening,
autoboxing and then
var-args.

SCJP6.0,My blog Ranchers from Delhi
Sven Mathijssen
Greenhorn

Joined: Aug 29, 2010
Posts: 19
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

Joined: Sep 07, 2010
Posts: 2
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

Joined: Mar 17, 2010
Posts: 1051

Welcome to JavaRanch.

For better understanding have a look at above posts.
Gari Jain
Ranch Hand

Joined: Jun 29, 2009
Posts: 100
Var-args is the last preference.
(K&B)

read the var-args topic


OCPJP 6-100%; Preparing for GATE11
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Vararg Problem
 
Similar Threads
Can you explain this with code...
difference between redefining and overridding
var-args question from skill test
Explanation needed: Method can be overridden , Attribute can not
Concurrency Question with Thread.sleep(5000) ?