0

Hi everyone,

I'm just wondering why this method below causes an infinite loop to happen. I thought it would run once and then stop. Anyone can explain this? that'll be great.

public String printer(String s){
		if (s.length() >= 5){
			System.out.println("I'm  " + s + " And my length = " + s.length());
		}
		return printer(s);
3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by rotten69
1

return printer(s); calls printer(s) again, which executes down to the last line where it executes return printer(s); etc infinitely

0

if we need to execute the loop only once there is no need of recursion
using simply return; will help you to get the desired output

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.