Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with Java Output Question!

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Help with Java Output Question!

    Hi, I was if someone could explain how this code works after it reaches the return line. I understand that the flow of control returns to the print statement after recurse(v). But I don't understand why it prints "wo" right after and why the code loops and prints "wor", "worl" and "world" instead of terminating.

    Thanks in advance!

    public class Recursion {
    public static void main(String[] args) {
    recurse("world");
    }
    public static void recurse(String w) {
    int l = w.length();
    if(l == 1) {
    System.out.println(w);
    return;
    }
    System.out.println(w);
    String v = w.substring(0, l-1);
    recurse(v);
    System.out.println(w);
    }
    }

    Output:
    world
    worl
    wor
    wo
    w
    wo
    wor
    worl
    world

  2. #2
    Junior Member
    Join Date
    Mar 2020
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java Output Question!

    Recursive methods get placed on the stack and it helps to think of them like a stack data structure.

    The key to understanding why it's printing after "wo" is in the following lines:

    System.out.println(w);
    String v = w.substring(0, l-1);
    recurse(v);
    System.out.println(w);

    Notice that it does the following:
    1. Prints the current word
    2. Gets a new word that is 1 character shorter
    3. Runs the recursive method on it
    4. AND THEN PRINTS THE ORIGINAL WORD AGAIN

    That last part is the key. After we've given the recurse(...) method a word of length-1 it exists the recursive loop and pops off the stack.
    By popping off the stack we return to wherever we left off in our code and continue executing the remaining lines.

    I'll try to comment below to show what's happening starting with the word "wo":

    System.out.println(w); // w is "wo"
    String v = w.substring(0, l-1); // v is now "w"
    recurse(v);
    System.out.println(w); // w is STILL "wo"

    Image: http://pasteall.org/pic/show.php?id=...bd07ddab091fff

    When a method exits it always has to return to where the calling code executed. In our case recurse(v) returns and the next line is a print statement.

    I hope this makes it a bit clearer.
    Last edited by lodenrogue; March 28th, 2020 at 07:42 PM. Reason: Clarity

Similar Threads

  1. [SOLVED] Java runtime get result output from prompt problem with a larger output in type
    By kingwang98 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2014, 08:52 AM
  2. Replies: 1
    Last Post: June 30th, 2014, 10:11 AM
  3. Question about Program Output
    By hopp22 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2014, 08:50 AM
  4. Very easy question about output.
    By ma5sacre in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 11th, 2014, 10:12 AM
  5. Question about formatting output
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2011, 11:57 AM

Tags for this Thread