0

how after the execution of if block in the whole program, the statement "System.out.println("after method showValue\t:"+ab);" is still able to fetch previous values ? i thought this statement will print 0 everytime but its not happening ?

public class Me{

public static void main(String[] args) {
    int ab=98;
    System.out.println(" value of ab in main at start\t:"+ab);
    Mno ref=new Mno();
    ref.showValue(ab);
    System.out.println("value of ab in Main After completion\t:"+ab);
}
}

class Mno{
    void showValue(int ab){
    System.out.println("Before method showvalue\t:"+ab);
    if (ab!=0){
        showValue(ab/10);}
    System.out.println("after method showValue\t:"+ab);

}

}

user7
  • 515
  • 1
  • 7
  • 17
  • You have created a class setup where you are testing the scope of the variable `ab` within the class `Mno` vs. the function `showValue`. – abiessu Sep 14 '13 at 14:42

1 Answers1

3

Java is pass-by-value, so in showValue() you're not handling the ab you have declared in your main() but rather dealing with a copy. Come to think of it, you're not even reassigning ab anywhere, so how could it possibly change?

In any case, you can see the pass-by-value concept at work in something like this:

public static void main(String[] args) {
    int n = 42;
    foo(n);
    System.out.println(n);
}

public static void foo(int n) {
    n = 0;
}
42

In foo(), we are reassigning a copy of n, not changing the n defined in main().


EDIT I see now that you're asking why the second print statement in showValue() doesn't print 0 each time it is reached. To see why, let's step through the function call by hand. This is what happens when you call that showValue(ab) in main():

  • Call function with argument of ab = 98.
  • Print 98 (1st print statement)
  • 98 != 0, so: (if-statement)
    • Call function again with argument of ab = ab/10 == 9.
    • Print 9 (1st print statement)
    • 9 != 0, so: (if-statement)
      • Call function again with argument of ab = ab/10 == 0.
      • Print 0 (1st print statement)
      • 0 == 0, so don't enter if-statement.
      • Print 0 (2nd print statement)
    • Print ab, which is 9 here. (2nd print statement)
  • Print ab, which is 98 here. (2nd print statement)
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • `Java is pass-by-value` I seriously doubt this statement. Can you rephrase it. – prasanth Sep 14 '13 at 14:53
  • @prasanth What do you mean you doubt that statement? I have an example in the answer that demonstrates it... – arshajii Sep 14 '13 at 14:54
  • Your statement holds only for primitive datatypes not for objects. They are `pass by copy of reference` – prasanth Sep 14 '13 at 14:55
  • @prasanth No it holds in every case. Java passes objects as references, and these references are *themselves* passed by value. By the way this is explained [here](http://stackoverflow.com/questions/40480/is-java-pass-by-reference). – arshajii Sep 14 '13 at 14:55
  • Thanks for the info. But 'references are themselves passed by value' part I could not understand :( – prasanth Sep 14 '13 at 14:57
  • but this statement System.out.println("after method showValue\t:"+ab); is able to fetch previous values of if block thats what my doubt is about. @ arshajii – user7 Sep 14 '13 at 16:45
  • @khwaish It prints `98` as expected. You never changed `ab`. – arshajii Sep 14 '13 at 17:11
  • @arshajii i guess i am not able to convince you my doubt. please go thorough the output once value of ab in main at start :98 Before method showvalue :98 Before method showvalue :9 Before method showvalue :0 after method showValue :0 after method showValue :9 after method showValue :98 value of ab in Main After completion :98 – user7 Sep 14 '13 at 17:14
  • @khwaish What do you expect that line to print? – arshajii Sep 14 '13 at 17:15
  • value of ab in main at start :98 Before method showvalue :98 Before method showvalue :9 Before method showvalue :0 after method showValue :0 after method showValue :9 after method showValue :98 value of ab in Main After completion :98 – user7 Sep 14 '13 at 17:15
  • i expected this line System.out.println("after method showValue\t:"+ab); to print 0 everytime but it is able to fetch previous values of ifblock – user7 Sep 14 '13 at 17:16
  • @khwaish Oh I see, I think I misunderstood your question, then. I'll edit to explain what happens there. – arshajii Sep 14 '13 at 17:17
  • @arshajii wow great finally u got my question :) i was confused how to ask,so was not able to frame my question properly – user7 Sep 14 '13 at 17:21
  • @arshajii Print ab, which is 9 here. (2nd print statement) Print ab, which is 98 here. (2nd print statement) how the jvm is able to fetch those previous values of ab(9 and 98).when at the end the value of ab is 0 – user7 Sep 14 '13 at 17:29
  • @khwaish Those values never changed. We just called the function again passing `ab/10` as an argument. The original `ab` still exists in the scope of the function. Each function invocation has an independent `ab` that is specific to that function. – arshajii Sep 14 '13 at 17:30
  • @arshajii now statisfied :) but now i m wondering when jvm calls showvalue(ab/10) again and again then how it manages previous values of ab.i mean by your statement "The original ab still exists in the scope of the function" does it mean that jvm stores previous values of ab somewhere – user7 Sep 14 '13 at 17:39