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.

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

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


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

Java Exception Handling - StackOverflowError, An examination of the Java StackOverflowError, including functional sample code showing how infinite recursion may cause StackOverflowErrors. package io.​airbrake; public class Main { public static void main(String[] args) { Iterator iterator = new Houses all logging methods for various debug outputs. You can have a look to Overloading Methods: Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists. Overloaded methods are differentiated by the number and the type of the arguments passed into the method.

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

[Solved] java.lang.StackOverflowError, When a method call is invoked by a Java application, a stack frame is out the recursion terminating condition to create StackOverFlowError. With method overloading you're calling "the same method", only with different parameters and/or different output. This makes it easy for you to write methods with the same core functionality but with different input parameters. Example: public int Sum(int a, int b) { return a + b; } public double Sum (double a, double b) { return a + b; }

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.

What actually causes a StackOverflow error in Java?, java.lang.StackOverflowError A StackOverflowError is simply signals that there is no more memory The common cause for a stack overflow is a bad recursive call. It is a list of the method calls that the application was in the middle of when an Exception was thrown. Why does Java not support operator overloading? In Java, two or more methods can have same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example: void func() { } void func(int a) {

java.lang.StackOverflowError, A method may not declare such errors in its throw clause, because This example is a very good example of how recursion can cause problems, if not implemented with caution. 3. More about the java.lang.stackoverflowerror. In java, Method Overloading is not possible by changing the return type of the method only. 1) Method Overloading: changing no. of arguments. In this example, we have created two methods, first add() method performs addition of two numbers and second add method performs addition of three numbers.

Groovy: java.lang.StackOverflowError When Implementing equals , However, when I perform an equality check, I get java.lang.StackOverflowError exception. I tend to see this exception when I write my too-smart-for-production StackOverflowError exception is because Groovy overloads == with equals() . The hashCode method itself is generated by eclipse guava plugin  Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.

StackOverflowError Vs OutOfMemoryError In Java, Stack is used for execution of methods and heap is used to store the objects. When the Stack becomes full, JVM throws java.lang. The objects you create in java are stored in the heap memory. ⇢Overloading Vs Overriding Method overriding is used to provide the specific implementation of the method that is already provided by its super class. Method overloading is performed within class. Method overriding occurs in two classes that have IS-A (inheritance) relationship. In case of method overloading, parameter must be different.

  • getSet(number) is calling itself
  • why would a recursive call trigger a compiler warning?
  • you produced and endless recursion, getSet() calls itself
  • Now I know mistake, I thought of producing Set of single item by calling method annotated with A.
  • @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"?
  • Yes, this is what I wanted. I made mistake.