• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

widening & boxing with var-args

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



A). Compilation fails
B). Output: Widening with var args
C). Output: Boxing with var args


Which would you choose and why?
[ May 26, 2006: Message edited by: warren li ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.

Reference:15.12.2.5 Choosing the Most Specific Method.
 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by wise owen:
A.

Reference:15.12.2.5 Choosing the Most Specific Method.



Very clearly explained. I'll read it when I want to fall asleep quickly.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes there are no explanations on how javac chooses the specific method.

This compiles and produces "Widening with var args" when run:

public class TestVarargs{
static void take( long... nums ){
System.out.println( "Widening with var args" );
}
static void take( Short... shorts ){
System.out.println( "Boxing with var args" );
}

public static void main( String[] args ){
byte s1 = (short) 23;
Short s2 = 45;

take( s1, s2 );
}
}


I will read the references provided above to see why my above compiles code while yours doesn't.
 
warren li
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can understand why your code compiles without the ambiguity.
Your this line " byte s1 = (short) 23;" means the 2nd method doesn't fit it any more.

But for my original code, both of the methods can run, and it seems the compiler can't determine the priority. It ends up with a compilation error.

My first reply without compiling it is "Widening with var args", coz the widening certainly existed before the autoboxing in Java 5. but I was wrong.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this priority scheme burned into my brain, saying 'widening >= boxing >= varargs'. Since widening has priority over boxing is expected this to just work. It turns out it is not. Nice post!

UPDATE: I've been playing with it a bit and it seems that even the following code is ambiguous according to javac:


So, it is not the combination of widening and var-args as the type (char) matches.
[ May 29, 2006: Message edited by: Dick Eimers ]
 
warren li
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good find, also.

but I am sorry to say your "'widening >= boxing >= varargs'" is wrong, even without my example.

certainly if you pass an 'int' to 'Long' wrapper, it won't work because it is Boxing first, then Widening: int -> Integer- not -> Long. Just to point out.

OK, back to my topic: my understanding( or the easy way to analyze this type of problem) is:

since they are already var-arg arguments, Java will already treat them at 1.5 Level. so within 1.5, both widening and auto-boxing are OK, so it is ambigious. But without the var-arg arguments, Java will evaluate the lower version(e.g. 1.4) first, under which situation it will choose widening over boxing, as widening exists before boxing.

I hope I am clear.
[ June 01, 2006: Message edited by: warren li ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Totally off topic, but Warren Li...did you ever live in south Florida?
 
You showed up just in time for the waffles! And this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic