As other answers noted, your function is not tail recursive, here is a tail recursive version of fibonacci:
long fibonacci(int n) {
if (n == 0)
return 0;
else
return fibonacciTail(n, 1, 0, 1);
}
long fibonacciTail(int n, int m, long fibPrev, long fibCurrent) {
if (n == m)
return fibCurrent;
else
return fibonacciTail(n, m + 1, fibCurrent, fibPrev + fibCurrent);
}
Also, the JVM does not do tail-call optimization so a stack frame will be allocated for every recursive call, making this quite an expensive operation. However it is important to note this is technically implementation dependent, see comments for link to IBM SDK that does TCO, and this SO question for more information.
An optimized version would be to do tail-call optimization manually, converting the above to a while loop with variable reassignment:
long fibonacciIter(int n) {
int m = 1;
long fibPrev = 0;
long fibCurrent = 1;
while (n != m) {
m = m + 1;
int current = fibCurrent;
fibCurrent = fibPrev + fibCurrent;
fibPrev = current;
}
return fibCurrent;
}