This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method overloading

 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry my friend but Type Erasure is not for arrays of primitives. Those types are reifilable.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. Karthick you should read some books about Generics
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"..........
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic