Java Reference
In-Depth Information
( input.hasNextLine() returns false ). The program has reached the very impor-
tant base case that stops this process from going on indefinitely. This version of the
method recognizes that there are no lines to reverse, so it simply terminates.
Then what? Having completed this call, we throw it away and return to where we
were just before executing the call:
public static void reverse(Scanner input) {
if (input.hasNextLine()) {
public static void reverse(Scanner input) {
String line - input.nextLine(),
if (input.hasNextLine()) {
public static void reverse(Scanner input) {
reverse(input),
String line - input.nextLine(),
System.out.println(line),
if (input.hasNextLine()) {
public static void reverse(Scanner input) {
if (i nput.hasNextLine()) {
String line = input.nextLine();
reverse(input);
System.out.println(line);
}
}
line "no?"
reverse(input),
}
String line - input.nextLine(),
System.out.println(line),
}
line “this”
reverse(input),
}
System.out.println(line),
}
line “this”
}
}
line “this”
We've finished the call on reverse and are positioned at the println right
after it, so we print the text in the line variable ( " no? " ) and terminate. Where
does that leave us? This method has been executed and we return to where we
were just before:
public static void reverse(Scanner input) {
if (input.hasNextLine()) {
public static void reverse(Scanner input) {
String line - input.nextLine(),
if (input.hasNextLine()) {
public static void reverse(Scanner input) {
if (i nput.hasNextLine()) {
String line = input.nextLine();
reverse(input);
System.out.println(line);
}
}
line "fun"
reverse(input),
String line - input.nextLine(),
System.out.println(line),
reverse(input),
}
System.out.println(line),
}
line “this”
}
}
line “this”
We then print the current line of text, which is " fun " , and this version also goes
away:
public static void reverse(Scanner input) {
if (input.hasNextLine()) {
public static void reverse(Scanner input) {
if (i nput.hasNextLine()) {
String line = input.nextLine();
reverse(input);
System.out.println(line);
}
}
line "is"
String line - input.nextLine(),
reverse(input),
System.out.println(line),
}
}
line “this”
 
Search WWH ::




Custom Search