Forum Stats

  • 3,851,507 Users
  • 2,263,992 Discussions
  • 7,904,751 Comments

Discussions

can anyone explain from the 3rd line of the output of this java program.?

User_2HV0M
User_2HV0M Member Posts: 1 Green Ribbon

class Wow{

public static void main(String[] args) {

int ab = 98;

System.out .println("ab in Main Before\t:" + ab);

Mno ref = new Mno();

ref.showValue(ab);

System.out .println("ab in Main After\t:" + ab);

}

}

class Mno {

void showValue(int ab) {

System.out .println("Before showValue\t:" + ab);

if (ab != 0)

showValue(ab / 10);

System.out .println("After showValue \t:" + ab);

}

}





output

ab in Main Before :98

Before showValue :98

Before showValue :9

Before showValue :0

After showValue :0

After showValue :9

After showValue :98

ab in Main After :98

Tagged:

Best Answer

  • Ana Kalemi
    Ana Kalemi Member Posts: 7 Green Ribbon
    Answer ✓

    The value of the int ab will remain the same in the main method, despite of being passed as a method parameter in showValue(ab) (line 6).

    Primitive variables are directly stored in stack memory. Whenever any variable of primitive data type is passed as an argument, the actual parameters are copied to formal arguments and these formal arguments accumulate their own space in stack memory.

    That goes to explaining the first and last line of output (ab will not change its initial value):

    >ab in Main Before :98

    >ab in Main After :98

    (first execution of method) Once inside showValue method, the fist line of the method will run, printing out:

    >Before showValue :98

    The if statement will evaluate to true since 98 ≠ 0, so a recursive call of itself is encountered. (second execution of method) As a parameter (98/ 10 = 9) is going to be passed. This time the first line inside the method will print out the current value of ab, which is 9:

    >Before showValue :9

    Again the condition evaluates to true since 9 ≠ 0 so, another recursive call follows (third execution of method), passing as a parameter (9/10 = 0). The first line will print out the current value of ab which is 0:

    >Before showValue :0

    This time the condition will evaluate to false since 0 ≠ 0 is not true. The recursive call will be skipped, so the last line of the method will be executed printing out the current value of ab which is 0:

    >After showValue :0

    Since on the second execution of the method the recursive call (a.k.a the third execution of method) is done, it goes on to executing the next line, which will print the current value of ab (second execution) which is 9:

    >After showValue :9

    Then, in the same manner for the first execution, after the recursive call is done executing, the program will move on to the next line printing out the current value of ab (first execution) which is 98:

    >After showValue :98

    User_2HV0M

Answers

  • Ana Kalemi
    Ana Kalemi Member Posts: 7 Green Ribbon
    Answer ✓

    The value of the int ab will remain the same in the main method, despite of being passed as a method parameter in showValue(ab) (line 6).

    Primitive variables are directly stored in stack memory. Whenever any variable of primitive data type is passed as an argument, the actual parameters are copied to formal arguments and these formal arguments accumulate their own space in stack memory.

    That goes to explaining the first and last line of output (ab will not change its initial value):

    >ab in Main Before :98

    >ab in Main After :98

    (first execution of method) Once inside showValue method, the fist line of the method will run, printing out:

    >Before showValue :98

    The if statement will evaluate to true since 98 ≠ 0, so a recursive call of itself is encountered. (second execution of method) As a parameter (98/ 10 = 9) is going to be passed. This time the first line inside the method will print out the current value of ab, which is 9:

    >Before showValue :9

    Again the condition evaluates to true since 9 ≠ 0 so, another recursive call follows (third execution of method), passing as a parameter (9/10 = 0). The first line will print out the current value of ab which is 0:

    >Before showValue :0

    This time the condition will evaluate to false since 0 ≠ 0 is not true. The recursive call will be skipped, so the last line of the method will be executed printing out the current value of ab which is 0:

    >After showValue :0

    Since on the second execution of the method the recursive call (a.k.a the third execution of method) is done, it goes on to executing the next line, which will print the current value of ab (second execution) which is 9:

    >After showValue :9

    Then, in the same manner for the first execution, after the recursive call is done executing, the program will move on to the next line printing out the current value of ab (first execution) which is 98:

    >After showValue :98

    User_2HV0M