Method overloading produce java.lang.StackOverflowError

Method overloading produce java.lang.StackOverflowError



As I know java has method overloading feature, so I am interesting why method annotated as B produce java.lang.StackOverflowError. I think this might be connected to some recursive call, but I did not have any compiler warnings. Can someone explain why I got exception.


B


java.lang.StackOverflowError


public static void main(String args)
Set<Integer> set = getSet(1);


//A
private static Set<Integer> getSet(List<Integer> numbers)
return new HashSet<>(numbers);


//B throwing exception
private static Set<Integer> getSet(Integer number)
return getSet(number);





getSet(number) is calling itself
– Eran
Sep 5 '18 at 12:53


getSet(number)





why would a recursive call trigger a compiler warning?
– Stultuske
Sep 5 '18 at 12:53





you produced and endless recursion, getSet() calls itself
– ItFreak
Sep 5 '18 at 12:58





Now I know mistake, I thought of producing Set of single item by calling method annotated with A.
– Bartek
Sep 5 '18 at 12:59






@Eugene Not sure if I agree. In the end, the problem is: a method is calling itself. When the OP really understands that, what is the purpose of the question then? That we debug his code for him? Do you debug NPE questions, or do you point to that famous DUP target? Do we hand out fish here, again and again and again, or try to educate folks how to fish, and tell them: "look over here, were you can learn how to fish"?
– GhostCat
Sep 5 '18 at 13:13





3 Answers
3



This:


private static Set<Integer> getSet(Integer base)
return getSet(base);



calls itself; you probably want to call it with a List:


List


return Arrays.asList(base); // or List.of(base) since java-9



Just look at your getSet method. You call it once, it calls itself again.



Maybe you wanted to do this?


private static Set<Integer> getSet(Integer base)
return getSet(Arrays.asList(base));





Yes, this is what I wanted. I made mistake.
– Bartek
Sep 5 '18 at 12:54



You run in infinite calls to private static Set<Integer> getSet(Integer number). First call is from public static void main(String args) After this the method invokes itself without any check to break the sequence of self calls


private static Set<Integer> getSet(Integer number)


public static void main(String args)


private static Set<Integer> getSet(Integer number)
return getSet(number);



getSet is overloaded but number being of type Integer calls itself. This becomes infinite sequence of same method calls and as each method invoke results in an entry in stack (to store the local states of the method call), stack needs memory which has a threshold , which gets exhausted after a certain number of method calls resulting in StackOverflowException.


getSet


number


Integer


StackOverflowException



Also there is nothing wrong in such calls if we look from the perspective of a Compiler. Its only that there must be a conditional check whcih can prevent the recursion turning into infinite recursion.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)