-2

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.

    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);
    }
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Bartek
  • 2,109
  • 6
  • 29
  • 40
  • 2
    `getSet(number)` is calling itself – Eran Sep 05 '18 at 12:53
  • 2
    why would a recursive call trigger a compiler warning? – Stultuske Sep 05 '18 at 12:53
  • 1
    you produced and endless recursion, getSet() calls itself – ItFreak Sep 05 '18 at 12:58
  • Now I know mistake, I thought of producing Set of single item by calling method annotated with A. – Bartek Sep 05 '18 at 12:59
  • @GhostCat the OP *knows* what a SO error is... there has to be a different duplicate here, if any – Eugene Sep 05 '18 at 13:02
  • 1
    @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 05 '18 at 13:13

3 Answers3

4

This:

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

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

return Arrays.asList(base); // or List.of(base) since java-9
Eugene
  • 117,005
  • 15
  • 201
  • 306
4

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));
    }
0

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) {
    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.

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.

nits.kk
  • 5,204
  • 4
  • 33
  • 55