Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Variable Length Arguments

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class OverLoadingTest {

public static void overloaded(int... enteros) {
System.out.println("int");
}

public static void overloaded(double... enteros) {
System.out.println("double");
}

public static void main(String[] args) {

}
}

Hi, this is my first post, i just want to be confirmed that I couldn't execute the first method regardless what i do. Sorry for my english
 
author
Posts: 23912
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

Julio Gonzales wrote:i just want to be confirmed that I couldn't execute the first method regardless what i do.



Why not?

Henry
 
Julio Gonzales
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because if I use overloaded(12); then the compiler says it is ambigous
 
Julio Gonzales
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



My question would be better in this way "How can i execute the first method without the compiler error", Thanks
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use non-primitives for the argument types.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also try overloaded(12.0), or overloaded(12D). Jim ...
 
Henry Wong
author
Posts: 23912
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

Julio Gonzales wrote:Because if I use overloaded(12); then the compiler says it is ambigous



Interesting. This seems completely broken to me. I am sure there is an obscure reason in the JLS -- but broken nonetheless.

Henry
 
Henry Wong
author
Posts: 23912
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

Julio Gonzales wrote:
My question would be better in this way "How can i execute the first method without the compiler error", Thanks



If you really want to do it... I guess one option would be...

overloaded(new int[] {12});


Henry
 
Julio Gonzales
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:You could also try overloaded(12.0), or overloaded(12D). Jim ...


Well , that was and example. My real question was "How I can execute the first method, without the compiler error". Thanks anyway =)
 
Julio Gonzales
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Julio Gonzales wrote:
My question would be better in this way "How can i execute the first method without the compiler error", Thanks



If you really want to do it... I guess one option would be...

overloaded(new int[] {12});


Henry



Mmm ok, thanks Henry. In that case, it is imposible to do it as literal ints. My question now it is
why the compiler aparently don't use "the most specific method" if the number it is perfectly match as an int before than a double?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like he said, this appears broken, unless I'm missing something obvious.

I'm assuming it has something to do with the autoboxing to an Object array, but w/o a JLS in front of me, I wouldn't know what.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, not so fast. To answer your first question, the following
code seems to 'int' along just fine. (It's pretty much what
Henry said.) And this approach works fine for double too.
Jim ... ...
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't explain the broken, though.
 
Julio Gonzales
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:Oh, not so fast. To answer your first question, the following
code seems to 'int' along just fine. (It's pretty much what
Henry said.) And this approach works fine for double too.
Jim ... ...



Yep, =) thanks my first question is answered
 
Julio Gonzales
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Doesn't explain the broken, though.



That's right, now I know how to execute the first method. The question in this moment is



"why the compiler aparently don't use "the most specific method" if the number it is perfectly match as an int before than a double?"

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Julio Gonzales wrote:Because if I use overloaded(12); then the compiler says it is ambigous



I haven't worked with varargs so I don't know if 0 parameters can be passed to a vararg. But if it's possible you can try this,

public static void overloaded(int one, int... enteros) {
public static void overloaded(double one, double... enteros) {
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tasha: I already tried that, and it does the same thing if you pass a single integer.

Also @Tasha, please see the JavaRanch naming policy. Please change your display name to conform with this policy. Thanks!
 
Henry Wong
author
Posts: 23912
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

I still think it is broken.

At first, I thought it had to do with the fact that var-arg conversions are done independent of method call conversions, with the var args happening last. With this, we can argue that widening can occur prior to the var-arg.... causing the ambiguity.

But... identity conversions has priority over widening conversions. So, if the compiler can see the element in the array for widening, it should see the element for identity too.

Still confused.
Henry
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry's suspicion is correct, looks like this bug[1] was reported in 2004 and is still not fixed

[1] http://bugs.sun.com/view_bug.do?bug_id=6199075
 
Sha Jar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:@Tasha: I already tried that, and it does the same thing if you pass a single integer.

Also @Tasha, please see the JavaRanch naming policy. Please change your display name to conform with this policy. Thanks!



I changed to my Earth name Natashia. :)
 
Julio Gonzales
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, it's broken, thanks to all
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic