5

Is this a good example to show tail recursion?

public printName(){
    System.out.println("Smith");
    printName();
}

I'm not intending to do this in real life, but i put this as an example for my exam. Is this one correct?

Nate
  • 31,017
  • 13
  • 83
  • 207
user1419170
  • 53
  • 1
  • 2
  • 7

3 Answers3

20

No, for two reasons:

  • tail recursion is only valuable when compiler supports it (tail call optimization). In Java it will still end with StackOverflowError

  • it would be nice to show some stop condition. Your code is equivalent to loop running forever.

Consider almost identical code in Scala, the only difference is that Scala compiler will perform tail call optimization and the loop runs forever:

def printName() {
  println("Smith"); 
  printName()
}
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 4
    I don't know: tail recursion is just tail recursion. The compiler can optimize it, but it is besides the point. Correct me if I'm wrong. – nhahtdh Jul 21 '12 at 13:59
  • 1
    @nhahtdh: the OP is not asking whether this is tail recursion or not (no doubt in that). He is asking whether this is a **good example** of tail recursion. IMHO it's not. – Tomasz Nurkiewicz Jul 21 '12 at 14:02
  • 2
    According to [Wikipedia](http://en.wikipedia.org/wiki/Tail_call) and my own CS memories it does not matter whether or not the language handles tail recursion specially - the example is tail-recursive. – A.H. Jul 21 '12 at 14:03
  • @A.H.: the fact that the last statement is a recursive call doesn't make tail recursion so important (for the same reason we are not teaching about *head recursion*) - it's the fact that it can be optimized to simple loop. Showing an example of tail recursion on a language that does not support it is misleading - it won't buy you anything in Java. So it's *not* a good example without extra remark ("*note that in Java...*") - so why not show it in pseudo-code or in a language where tail-recursion is actually meaningful? Otherwise you'll see Java developers *thinking* they gained something – Tomasz Nurkiewicz Jul 21 '12 at 14:33
15

A better example of tail recursion would be something like this:

public printName(int level){
    if( level <= 0 )
         return;
    System.out.prntln("Smith");
    printName(--level);
}

This examples includes the important part where the recursion is terminated.

Besides of this: As other answers already noted: Since Java does not optimize tail-recursion, there is no point in using it in this language. So you basically end up to optimize your algorithm yourself - by making it iterative. That's the point of tail-recursion: It can be proved, that any tail-recursive algorithm can be transformed into an iterative algorithm.

A.H.
  • 63,967
  • 15
  • 92
  • 126
1

I would say this is an example of tail recursion, as you recurse in the tail of the program :) I do however not think that the JVM will optimize this, which is probably what you want.

Jonatan
  • 2,734
  • 2
  • 22
  • 33