user2830571
user2830571

Reputation: 71

Practice it! 1.2.3: Strange

I am new to learning Java and I do the Practice-It! questions to help my understanding of the language.

I am stuck on problem 1.2.3, titled Strange and in this question they want you to type out the output based on the code they provide.

My question is, I don't understand the output in comparison to the input.

 public class Strange {

 public static void main(String[] args) {
    first();
    third();
    second();
    third();
 }

 public static void first() {
    System.out.println("Inside first method.");
}

public static void second() {
    System.out.println("Inside second method.");
    first();
 }

 public static void third() {
    System.out.println("Inside third method.");
    first();
    second();
 }
}

I thought the output would be :

Inside first method.
Inside third method.
Inside first method.
Inside second method.
Inside second method.
Inside first method.
Inside third method.
Inside first method.
Inside second method.

But it is:

Inside first method.
Inside third method.
Inside first method.
Inside second method.
Inside first method.
Inside second method.
Inside first method.
Inside third method.
Inside first method.
Inside second method.
Inside first method.

Why is this?

Thanks very much.

Upvotes: 1

Views: 3158

Answers (6)

Ashwin Parmar
Ashwin Parmar

Reputation: 3045

You should understand this by looking at this step by step calling of each method execution.

first();
    Inside first method.
third();
    Inside third method.
        first();
            Inside first method.
        second();
            Inside second method.
                first();
                    Inside first method.
second();
    Inside second method.
        first();
            Inside first method.
third();
    Inside third method.
        first();
            Inside first method.
        second();
            Inside second method.
                first();
                    Inside first method.

Upvotes: 5

Bruno
Bruno

Reputation: 122659

public static void first() {
    System.out.println("Inside first method.");
}

public static void second() {
    System.out.println("Inside second method.");
    first();
}

Every call to second() will display the following lines consecutively (because first() is called immediately after):

Inside second method.
Inside first method.

This is also this only way "Inside second method." can be printed. This output you expect is therefore not possible:

...
Inside second method.
Inside second method.

Upvotes: 2

Imtiaz Zaman Nishith
Imtiaz Zaman Nishith

Reputation: 490

There is nothing wrong with the output. Just check and recheck again. the output will

Inside first method.
Inside third method.
Inside first method.
Inside second method.
Inside first method.
Inside second method.
Inside first method.
Inside third method.
Inside first method.
Inside second method.
Inside first method.

thanks.

Upvotes: -1

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298908

You can understand it by applying some indentation:

first
third
    first
    second
        first
second
    first
third
    first
    second
        first

(Inner nodes represent method calls by outer nodes)

Upvotes: 7

Srikanth
Srikanth

Reputation: 1956

Your secondMethod() has a call to firstMethod() that's why you see a "Inside First Method" right after every "Inside Second Method". Remove the call of firstMethod() inside secondMethod() and you will see your expected output

Upvotes: 1

Angelo Fuchs
Angelo Fuchs

Reputation: 9941

You should start a debugger and step through that code, so you will see what it does when.

The problem in explaining things here, is that it does what it should do (so the programs output is what I would expect).

You can find you debugger in the IDE that you are (hopefully) using. You will have to add a breakpoint first(); here and then should be able to "Step into" every method you want to, or "Step over" the System.out.println.

Upvotes: 3

Related Questions