James
James

Reputation: 1289

Stack overflow with tail recursion

Why am I getting a stack overflow exception? Isn't this meant to be a tail recursive function?

public static int tailFact(int n, int mult) {
    if(n == 0) {
        return mult;
    }else {
        return tailFact(n-1, n*mult);
    }
}

public static int factT(int n) {
    return tailFact(n, 1);
}

public static void main(String[] args) {            
            factT(100000);
}


/*Exception in thread "main" java.lang.StackOverflowError

  at test3.Test.tailFact(Test.java:13)
  at test3.Test.tailFact(Test.java:13)
  ...
*/

Upvotes: 0

Views: 73

Answers (1)

James
James

Reputation: 1289

Java doesn't support tail recursion.

Upvotes: 4

Related Questions