Method Overloading with new features of Java 1.5
I was playing with Autoboxing and Ver-args features of Java 1.5 and found interesting results with method overloading.
If you have a class which has a method void hello(Integer x) and you call hello(5) then Autoboxing will come into play may and method hello will be called.
Another case is, If you have a method void hello(long x) and you call
hello(5) then primitive integer 5 will be widened to long and method hello will be called.
Now suppose you have hello method overloaded in your class. Lets look at this example in detail.
public class methodOverloading{
public static void hello(Integer x){System.out.println("Integer");}
public static void hello(long x){System.out.println("long");}
public static void main(String[] args){
int i = 5;
hello(i);
}
}
Guess what would be output when we execute this java program. One can imagine of two answers. If you think Autoboxing will be done then "Integer" would be printed. But if you think widening of integer will take place then "long" would be printed. The only reason is, Sun did not want already existing programs to get different results, when they switch to Java 1.5. With earlier Java versions, widening would have taken place and long would have been printed, so is happening with Java 1.5 as well.
The question is, given that both methods exist, which one will be used? In other words, which one compiler think more desirable, Autoboxing or widening? The answer is that the compiler will choose widening over boxing, so the output would be "long".
Compiler can choose to perform following three operations
- Widening
- Autoboxing
- Var-args
At any point in time there are two possible options like in above example then compiler would choose; "Widening over Autoboxing", "Widening over Var-args", "Autoboxing over Var-args".
Now the question is, what if there is a combination of these operations required? To get an answer of this question one needs to understand how widening works with objects. Widening in objects only in IS-A relation. If class Horse extends Animal then Horse can be widened to Animal for that reason any class can be widened to Object. Can Integer be widened to Long? Answer is NO because there is no IS-A relation there.
When there is a combination of operations required compiler will perform BoxingAndWidening but it will not perform WideningAndBoxing. It is just too much work for the compiler.
class WidenAndBox{static void go(Long x){
System.out.println("This is from long");
}
public static void main(String[] args){
byte b = 5;
go(b);
}
}
Here Compiler needs to Widen byte to long first and then It can be boxed to Long which is not allowed (Searching for reason, Any references are welcome).
class BoxAndWiden{
static void go(Object x){
System.out.println("This is from object");
}
public static void main(String[] args){
byte = 5;
go(b);
}
}
Here compiler needs to be boxed into Byte first and then can be widen to Object because it follows IS-A relation.
This is it from my side. Feel free to share your ideas on this.
Labels: Autoboxing, IS-A relation java, Java 1.5, method overloading, Var-args, Widening

10 Comments:
It is good...
Hi! its good..
but the topic is not covered thoroughtly...
you have explained that complier choose "BoxingAndThenWidening" if it is needed..
but what if i have a two methods like
public void test(Integer... x){ }
public void test(Number n1, Number 2){ }
and invoking the method
test(1, 2)
so what complier will choose??
"BoxingAndWidening" or "BoxingAndVarArgs" ???????
--------------------------
another scenario:
public void test(int... x){ }
public void test(Number n1, Number n2){ }
and calling...
test(1, 2);
what will compiler choose;
"WideningAndVarArgs" or "BoxingAndWidening"
----------------------------
another scenario:
public void test(int... x){ }
public void test(Number... x){ }
and calling...
test(1, 2);
will it compile??? successfully??
and if compiled then which method will be invoked???
in short, "BoxingAndWindeningAndVarArgs" or "WideningAndVarArgs" ????
Brim over I acquiesce in but I think the post should have more info then it has.
I wish not acquiesce in on it. I assume warm-hearted post. Specially the title-deed attracted me to study the whole story.
Nice dispatch and this enter helped me alot in my college assignement. Thanks you on your information.
[url=http://vegasonlines.net/online-casino-affiliate-program.html]casinos de la provincia de cordoba [/url]
[url=http://vegasonlines.net/computer-games-casino.html]salsa casino [/url]
[url=http://vegasonlines.net/lake-charles-casino.html]casino costa blanca [/url]
[url=http://vegasonlines.net/bwin-casino.html]borgatta casino atlantic city [/url]
[url=http://vegasonlines.net/borgata-casino-and.html]atlantic city casino show [/url]
[url=http://vegasonlines.net/casino-phoenix-arizona.html]casino online cn bonus [/url]
[url=http://vegasonlines.net/casino-orillia-ontario.html]casino video poker [/url]
[url=http://vegasonlines.net/casino-no-deposit-no.html]best no deposit casino bonuses [/url]
[url=http://vegasonlines.net/hard-rock-casino-fort-lauderdale.html]juegos de casino para descargar [/url]
[url=http://vegasonlines.net/punta-cana-casino-resort.html]descarga juego on line casino gratis [/url]
[url=http://vegasonlines.net/cyber-casino.html]casino slot game [/url]
[url=http://vegasonlines.net/mystic-lake-casino-minnesota.html]casino descarga online [/url]
[url=http://vegasonlines.net/hollywood-casino-tunica.html]comprar dvd baile salsa rueda casino [/url]
[url=http://vegasonlines.net/casino-argosy.html]reno silver legacy resort casino [/url]
[url=http://vegasonlines.net/casino-for-fun.html]mueble juego casino [/url]
ocio juego casino
[b]steve wynn casino[/b]
10 best online casino
casino aranjuez actuacion
[b]playas de casino brasil[/b]
casino estoril portugal
[u]casino bahia cadiz[/u]
jeux casino sur net
aladdin resort and casino home page
[b]internet casino gambling game[/b]
catarata niagara bear den at seneca niagara casino
casino online american casino
[url=http://seghan.ru/go.php?sid=35][img]http://s002.radikal.ru/i200/1001/f0/0ddd2280d906.jpg[/img][/url]
[url=http://vonzura.misslich.de/sitemap.html]and buy cigarettes [/url]
buy roll in cigarette filter tips camel cigarettes reynolds mail order to buy cheap cigarette
[url=http://deuaexj.naheliegend.de/]age to buy cigaretts [/url]
cigarette buying age buy cheap cigarette newport buy cigarettes from south carolina
[url=http://botziho.485.at/sitemap.html]buy marlboro cigarette [/url]
where can i buy davidoff cigarettes zibb cigarettes buy buy cigarettes paypal
[url=http://deuaexj.naheliegend.de/]buy cigarettes cheap online [/url]
buy cigarettes at a buy cigarettes online illinois buy cigarettes over the world
[url=http://deuaexj.naheliegend.de/]red apple cigarettes buy [/url]
buy epique cigarettes buying cigarettes in virginia where to buy quest cigarettes
[url=http://powjxun.wieder-mal.de/sitemap.html]buy indian cigarettes ontario [/url]
buying and cigarettes or tobacco and law order cigarettes from new york buy cigarettes on internet
[url=http://mlwqdwu.xn--baldmglichst-8ib.de/]where can i buy candy cigarettes [/url]
buy cigarettes at 16 buy cigarettes online legally buy hassan cigarette cards
[url=http://ubpscsn.pro.ac/sitemap.html]buy camel blue cigarettes [/url]
zibb cigarettes buy buy cigarettes online native american buy cigarettes online from discount store
[url=http://botziho.485.at/]buy belmont cigarettes online [/url]
buying russian cigarettes on line buy newport cigarettes online by visa buy barclay cigarettes
[url=http://tioozua.connection24.de/sitemap.html]to buy cigarette cases [/url]
how to buy cigarettes cheaper law on buying cigarettes buy some candy and cigarettes
Today is my lucky day :)
Apple is giving review copies of iPad to 100 lucky person. Go to http://bit.ly/d9QOON and apply for it.
Hello. My wife and I bought our house about 6 months ago. It was a foreclosure and we were able to get a great deal on it. We also took advantage of the 8K tax credit so that definitely helped. We did an extensive remodeling job and now I want to refinance to cut the term to a 20 or 15 year loan. Does anyone know any good sites for mortgage information? Thanks!
Mike
Good day
This is the big one. Finding http://www.BuySellDirect.net is going to change the way I do business, selling FREE in an advance ebay like will never be the same again.
http://www.BuySellDirect.net free service is the best of all the rest to sell your products and services on the Internet.
Post a Comment
Subscribe to Post Comments [Atom]
Links to this post:
Create a Link
<< Home