0
我最近了解到尾遞歸。 據我所知,許多編程語言編譯器在發現遞歸方法爲「尾遞歸」時執行[當前java不],代碼優化。Java尾遞歸:低於斐波那契碼尾遞歸?
我對TR的理解: 編譯器,在調用返回後沒有進一步的操作時,不會創建新的堆棧幀(取代舊的調用的堆棧幀)。
下面的代碼[即使在java]尾部遞歸?
假設totalSeriesLenght = 10
public void generateFibonacciSeries(int totalSeriesLenght) {
int firstNum = 0;
int secondNum = 1;
printNextFibonacciNumber(firstNum, secondNum,totalSeriesLenght);
}
public void printNextFibonacciNumber(int fiboOne , int fiboTwo,int totalSeriesLenght) {
if(totalSeriesLenght >= 1) {
System.out.print(fiboOne + ",");
int fiboNext = fiboOne + fiboTwo;
totalSeriesLenght --;
printNextFibonacciNumber(fiboTwo, fiboNext,totalSeriesLenght);
}
}
Java不會爲尾遞歸執行任何代碼優化:http://stackoverflow.com/questions/3616483/why-does-the-jvm-still-not-support-tail-call-optimization – Tunaki