• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

regarding overloaded methods of var args

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code written by me:



It does not compile and says that reference to doStuff is ambiguous. i do not understand it because as far as i know , for overloaded methods the compiler goes first for the exact match. if i pass an int , then also it gives the same problem .I think there is no ambiguity beacause when i pass int , i mean to invoke and when i pass Integer i mean to invoke . Why is the compiler confused?? can anyone clarify ??
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saima kanwal wrote:Why is the compiler confused?? can anyone clarify ??



Technically, neither one is an exact match. The first doStuff() method takes an int array. And the second doStuff() method takes an Integer array.

Neither one is an exact match of an Integer. The JLS, as it is currently written doesn't specify which is a better match. And hence, it is ambiguous. Maybe this will be fixed in a future release of Java -- as I agree, the second case (Integer --> Integer var-arg array) is the better candidate.

Henry
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please clarify the meaning of



According o my understanding it means the method doStuff takes 0 or more int values . That's why if we just delete the second overloaded method (taking Integer... ) and pass int i =1 ; to it this method is invoked and works fine. why?? does it mean that it should take an array of ints ?? if so then why does it work for a single or multiple int values without the array curly braces (when not overloaded)?
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saima kanwal wrote:why?? does it mean that it should take an array of ints ??



Why don't you tell us? Assuming that your doStuff() method will do more than just a println(), and actually use the parameters, how does your method access those parameters? For zero parameters? For one paramater? etc.

saima kanwal wrote:if so then why does it work for a single or multiple int values without the array curly braces (when not overloaded)?



Without what curly braces?

And the error is not a syntax error, it is an ambiguous error cause by the overloading. So, no overloading means error goes away.

Henry
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please refer to page 47 of K & B chapter 1 where var- arg methods are described. It says :

Let's look at some legal and illegal var-arg declarations:

Legal:
void doStuff(int... x) { } // expects from 0 to many ints as parameters
....
..
....



now the comments in the line means that this method can be invoked by passing zero or more int values like : doStuff(1,2);
doStuff(1);

By curly brace I mean that i am not passing it like this doStuff(new int[]{1,2,3}) // this is also valid for the same method declaration, and the ints without being inside an array are also valid . So I want to clarify myself with the meaning of var- arg method declaration?? what does it mean ?? int values or an array of int??
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saima kanwal wrote:what does it mean ?? int values or an array of int??



Again. Please tell us. You didn't answer my question.

When you call doStuff(1), in the doStuff() method, is the variable "i", an int or an int array?

Henry
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to my understanding, when i write :

doStuff(i) // i am passing an int value

and when i write

doStuff(new int[]{1,2,3}) or

int arr = {1,2,3};
doStuff(arr)

I am passing an int array.
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saima kanwal wrote:According to my understanding, when i write :

doStuff(i) // i am passing an int value

and when i write

doStuff(new int[]{1,2,3}) or

int arr = {1,2,3};
doStuff(arr)

I am passing an int array.




It would have taken you 1 minute to write a quick test to test it out. Certainly, much less time than the two hours of expecting me to answer it for you. And then guessing.

Henry
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Henry:Hi Henry ,

According to my understanding, when i write :

doStuff(i) // i am passing an int value

and when i write

doStuff(new int[]{1,2,3}) or

int arr = {1,2,3};
doStuff(arr)

I am passing an int array.



I have been trying whatever i was writing here , and i tried both of them, and both compile fine. I do not want to know whether it compiles or not( that is not my question, you misunderstood me ) , i can do that on my computer, and i did that. I want to understand the concept of var-arg , that is what i have been asking here for. I want to know why does the compiler accepts both the cases , though according to me both have different meaning :passing int values like doStuff(10,20) and passing an array like doStuff(new int[]{1,2,3}) .


@everyone on the forum: Can anybody answer my question and clarify me the concept of var-arg. To make it simpler: can anybody tell me what does the compiler want to get as the argument when i write doStuff(int... i)? it wants int values or int array? or both ?

and what is meant by doStuff(int[]... i)?? because it takes a single dimension and double dimension arrays . I have not found it anywhere clearly in K & B book , so want someone to clarify me the concept of var-args ( and not the compiler output)



when no overloaded methods are present: doStuff(int... i) is invoked when i pass either doStuff(10,20) or doStuff(new int[]{1,2,3})

when no overloaded methods are present: doStuff(int[]... i) is invoked when i pass either doStuff(new int[]{1,2}) or doStuff(new int[][]{ {1,2}, {1,}})

when both methods are present in a single class (overloaded methods) : and when i pass :
doStuff(10,20) ---------doStuff(int... i)
doStuff(new int[]{1,2,3})----doStuff(int... i)
doStuff(new int[][]{ {1,2}, {1,}})-----doStuff(int[]... i)


so now it got confusing for me . That's why i wanted to understand the basic concept of var-arg.

 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saima kanwal wrote:
I want to understand the concept of var-arg , that is what i have been asking here for. I want to know why does the compiler accepts both the cases , though according to me both have different meaning :passing int values like doStuff(10,20) and passing an array like doStuff(new int[]{1,2,3}) .



I am trying to get you to understand the concept, but you seem unwilling, or unable, to try it to figure it out yourself.

So, here is the answer.... with var-args, it is *always* passed an array. In your example, it is an int array. And it is an int array, regardless of whether you actually pass an int array, pass no variables, pass one variable, and any number of variables. It is *always* passed an int array. It takes an int array. Period.

You should really try it out yourself. Call the method with no parameters, and you will see that it is passed an array with no elements. Call the method with a single int parameter, and you will see that it is passed an int array of size one, that contains the int as an element. etc. I didn't ask you to see if it compiles. I asked you to write code to actually see this for yourself.


Which goes back to my original answer to your original question... The Java Language Specification doesn't clarify which has priority, going from an int to an int array, or going from an int to an Integer array. Hence, it is an error, because it is ambigious. Notice that going from an int to an int isn't one of the options.

Henry
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Saima ,

Cool . Henry is Correct.

1. with arument int[] ... i
------------------------------------------------------------------------------------
java Code:



and Decompiled Code[Java Decompiler]:



-----------------------------End - (1) ---------------------------

2. with argument int ... i

----------------------------------------------------------

Java Code:


and Decompiled Code[Java Decompiler]:




-------------------------- End - (2) -----------


Please go through the above code. Hope you will understand
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually i found that if the passed parameter can be hold by any other overloaded version of the method it causes the ambiguity...
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think part of the trouble understanding this is because there's a difference between what you "pass" to the method, and what the method "takes".

From Sun's vararg documentation:

It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process.

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.


(The or in italics is Sun's.)

The automagic feature converts the "sequence" that you passed, into the array that the method "takes". Or you can bypass this automagic feature and pass it an array directly.

Also the specific error appears to depend on which ambiguous method is first in the file, rather than the data type of the argument. If the (int... i) method appears first, that's the ambiguity error you'll see, whether you pass it an int or an Integer. (If (Integer... i) is first, that will be the error.) So, it never even gets to the point of deciding whether boxing or varargs "wins". I guess it's saying that since autoboxing effectively renders this distinction moot, you should do a better job distinguishing your overloads.
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic