简体   繁体   中英

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)
  ...
*/

Java不支持尾递归。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM