my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method overloading" Watch "Method overloading" New topic
Author

Method overloading

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
I am wondering why this compiles fine when placed inside the same class:


if called like output is In string.

However this does not:

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Meghna Bhardwaj wrote:I am wondering why this compiles fine when placed inside the same class:


if called like output is In string.

However this does not:



int[] and int... are considered the same by the compiler but String and StringBuffer are distinct objects so - no problem for the compiler.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Lukas Smith wrote:int[] and int... are considered the same by the compiler


I would like to add, int[] and int... are cconsidered same by the compiler in terms of overloading not in other scenarios ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Please see overloading in terms of method signatures. Signature of the method having the var arg int... is int[] So the compiler will not allow that. As for the String and StringBuffer, the method signatures are different as String is not a Stringbuffer or viceversa. Stringbuffer Stringbuilder and String are subtypes of Charsequence. And why it uses String is because the argument passed to the method is String literal and not a StringBuffer.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

Ankit Garg wrote:
Lukas Smith wrote:int[] and int... are considered the same by the compiler


I would like to add, int[] and int... are cconsidered same by the compiler in terms of overloading not in other scenarios ...





"int[]" and "int..." after the "TYPE ERASURE" process are one and the same.....

that is AT RUNTIME int[] and int... are the same......


i suppose you got a compiler error stating both have same type erasure...


KARTHICK.C , SCJP6-93%
(Born to Win)
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Sorry my friend but Type Erasure is not for arrays of primitives. Those types are reifilable.
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

i am not saying that it's for primitives......

its for normal array argument and the variable argument array.......

"[]" and "..." both are treated as same at Run time i suppose......

i got the "type erasure" compiler error when i tried the same thing.....that's why i mentioned it.....

correct me if i am wrong........


thanks in advance...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

karthick chinnathambi wrote:"int[]" and "int..." after the "TYPE ERASURE" process are one and the same.....

This problem does not have anything to do with type erasure. Type erasure has to do with generics, but in this case, there are no generics being used.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Exactly. Karthick you should read some books about Generics
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

karthick chinnathambi wrote:"int[]" and "int..." after the "TYPE ERASURE" process are one and the same.....

that is AT RUNTIME int[] and int... are the same.......


What I was trying to say was that if you try to overload int[] with int... or vice versa, you'll get an error. But they can't be used interchangeably. You can't write a method with int[] and expect it to work as a var-arg method ...
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

ya ....

i meant the same..........

"both have same type erasure"-----> is a compile time error only..............
obviously they can't be used interchangeably if the raise compile time errors...

At RUNTIME , i suppose both are same...........
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

There is no type erasures here. Its both have the same method signatures that's why compiler doesn't allow it.
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

Nitish Bangera wrote:There is no type erasures here. Its both have the same method signatures that's why compiler doesn't allow it.


the compiler error was like that only.......
"Both having same type erasures"..........
 
jQuery in Action, 2nd edition
 
subject: Method overloading
 
Similar Threads
Interface Concept in anonymous classes///
Please explain me the output in detail
preculiar synchronized behaviour
synchronization
Overiding class variable through inheritance