my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes coding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "coding problem" Watch "coding problem" New topic
Author

coding problem

Sahil Manchanda
Ranch Hand

Joined: Jul 24, 2012
Posts: 41

static void call(Integer... i){
System.out.println("hi"+i);
}
static void call(int... i ){
System.out.println("hello"+i);
}
public static void main(String... args){
call(10);
}



can somebody explain why this code does not work?
note: i have omitted the class and main method here!
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Compiler at compile time finds 10 is type of int as well as Integer,hence ambiguity.Compiler should identify that 10 is integer literal not Integer object but unfortunately it happens only at runtime not compile time.


OCA7
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18531
    
  40

ankita modi. wrote:Compiler at compile time finds 10 is type of int as well as Integer,hence ambiguity.Compiler should identify that 10 is integer literal not Integer object but unfortunately it happens only at runtime not compile time.


Actually, it is a bit more complex that that -- it has to do with the phases the compiler takes to find an matching method, whether a particular phase finds a matching method or not, and whether a particular phase finds more than one method, in which case, it must resolve the "most specific" one..... and unfortunately, in my opinion, it is in the most unreadable section of the java language specification.... argh!!!

Henry




Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sahil Manchanda
Ranch Hand

Joined: Jul 24, 2012
Posts: 41

Henry Wong wrote:
ankita modi. wrote:Compiler at compile time finds 10 is type of int as well as Integer,hence ambiguity.Compiler should identify that 10 is integer literal not Integer object but unfortunately it happens only at runtime not compile time.


Actually, it is a bit more complex that that -- it has to do with the phases the compiler takes to find an matching method, whether a particular phase finds a matching method or not, and whether a particular phase finds more than one method, in which case, it must resolve the "most specific" one..... and unfortunately, in my opinion, it is in the most unreadable section of the java language specification.... argh!!!

Henry





ya mate! sometimes its a Runtime exception but sometimes my compiler gives an error if i am using an IDE
 
 
subject: coding problem
 
Similar Threads
JavaRanch #19
var args method
varargs overloaded method
main method
try again