Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

coding problem

 
Sahil Manchanda
Ranch Hand
Posts: 41
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Sahil Manchanda
Ranch Hand
Posts: 41
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic