Cant understand why Recursion is taking place in the method

I cant seem to figure out why the below outputs136.

From what I understand the no arg constructor is called withmyvar value getting thegetValue method argument value of 17. The value of 17 is less than 100 so the value goes into the else statement where it doubles the value so it should be 34?Please explain exactly why the value is 136?ThegetValue method is not a for loop so why is it recursion taking place where it tests the value until its over 100?

publicclass Recursery

{

publicint myvar = -1;

public Recursery()

{

myvar = getValue(17);

}

publicint getValue(int myvalue)

{

if(myvalue > 100)

{

return myvalue;

}

else

{

return getValue(myvalue * 2);

}

}

publicstaticvoid main (String[] args)

{

Recursery r =new Recursery();

System.out.println(r.myvar);

}

}

[1802 byte] By [florida41a] at [2007-11-26 14:53:41]
# 1
Why don't you throw some printlns in there and see what's going on?
es5f2000a at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 2
And tell us what this line does:return getValue(myvalue * 2);
ChuckBinga at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 3

return getValue(myvalue * 2);

is returning the method with the argument multiplied times 2?

I put in println:

public class Recursery

{

public int myvar = -1;

public Recursery()

{

myvar = getValue(17);

}

public int getValue(int myvalue)

{

if(myvalue > 100)

{

System.out.println("OVER 100 ->" + myvalue);

return myvalue;

}

else

{

System.out.println("UNDER 100 ->" +myvalue);

return getValue(myvalue * 2);

}

}

public static void main (String[] args)

{

Recursery r = new Recursery();

System.out.println(r.myvar);

}

}

It is doing recursion in the getValue one arg method but dont know why it is doing that?

UNDER 100 ->17

UNDER 100 ->34

UNDER 100 ->68

OVER 100 ->136

136

florida41a at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 4
> And tell us what this line does:> return getValue(myvalue * 2);Again, try to dissect this line and explain what it's doing.
ChuckBinga at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 5
> It is doing recursion in the getValue one arg method> but dont know why it is doing that?Do you really not see any difference betweenreturn myValue;and return
cotton.ma at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 6

> > And tell us what this line does:

> > return getValue(myvalue * 2);

>

> Again, try to dissect this line and explain what it's

> doing.

Let's get a little more heavy-handed, in case you still don't see it. Ask yourself if this line of code calls any methods. If so, which ones does it call?

DrClapa at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 7

Thanks for the responses because I am still tryin to understand this.

I looked up Java return definition and it says:

"A Java language keyword used to end the execution of a method

and return program execution to the next method up the call stack.

It can return an optional value."

My attempt to understand is below in the comments:

public int getValue(int myvalue)

{

if(myvalue > 100)

{

return myvalue;//returns just one int data type variable called myvalue?

}

else

{

return getValue(myvalue * 2);

/*

"return getValue(myvalue * 2); "

this is inside the method getValue

and it is returning a call to itself with the argument

of myvalue argument times 2.

Then I thought it would stop because the return word means it is the end of the execution?

*/

}

}

florida41a at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 8

Yes, but if it's calling itself then what will happen? You start with 17, that is less than 100 so it calls itself passing the value (17 * 2). So it's called itself now, again, with a value of 34 which is less than 100 so it calls itself (again) passing the value (34 * 2). Once again 68 is less than 100 so it calls itself again passing (68 * 2). Now 136 is more than 100, so it returns the value which is 136, which returns to the previous call, which returns to the previous call, which finally returns to the main method with that value.

Remember "getValue(myValue * 2)" must be executed first. Whatever the value it returns we will return. However since in that method it executes itself again that must first execute and return too, and so on until it reaches the point where it's > 100 and can just flat out return the value and not invoke any new methods.

kablaira at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 9
Furthermore, if you're using an IDE that has a debugger and you can run through it line-by-line it might be easier to understand.
kablaira at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 10

> Thanks for the responses because I am still tryin to

> understand this.

>

Okay let's a try it a different way. First look at this...

public int getValue(int myvalue)

{

if(myvalue > 100){

return myvalue;

}

else

{

while(myvalue<=100)

{

myvalue = myvalue * 2

}

return myvalue;

}

}

Can you see how this works?

Now look at this.

public int getValue(int myvalue)

{

if(myvalue > 100){

return myvalue;

}

else

{

int doubledValue = myvalue * 2;

int valueToReturn = getValue(doubledvalue);

return valueToReturn;

}

}

Do you see how it works?

cotton.ma at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 11

Thanks so much for all the details!!!

This one makes the most sense to me because its in a while loop and will keep increasing the value until it meets the over 100 value:

public int getValue(int myvalue)

{

if(myvalue > 100){

return myvalue;

}

else

{

while(myvalue<=100)

{

myvalue = myvalue * 2

}

return myvalue;

}

}

For this one below, I guess I am confused that there is two different return values (myvalue and valueToReturn) and from what I understand the method can only return one return value and that has to be the parameter value (from this line: public int getValue(int myvalue)) which in this case would be int myvalue?

public int getValue(int myvalue)

{

if(myvalue > 100){

return myvalue;

}

else

{

int doubledValue = myvalue * 2;

int valueToReturn = getValue(doubledvalue);

return valueToReturn;

}

}

florida41a at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 12

> I guess I am confused that there

> is two different return values (myvalue and

> valueToReturn) and from what I understand the method

> can only return one return value and that has to be

> the parameter value (from this line: public int

> getValue(int myvalue)) which in this case would

> be int myvalue?

You can only return one value -- this is true. If you look at the return signature of the method, it just says int. In other words, you can return anything you want, as long as it's an int (variable or literal, like 3). It doesn't have to be the parameter value passed in.

kevjavaa at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...
# 13

Florida41,

For your last question see kevjava's reply.

Also I hope you understand that the two methods I showed and yours all do the same thing. This really was the point I was trying to make because you seemed lost in the flow of your method so I thought some other different examples would help you see the multiple ways of getting the same result.

If it helps the recursion portion of your program is exactly like the while loop. While the number is less than 100 keep doubling.

In the second example I was trying to split up the method call and the return statement onto two lines. Because when you had it on one line, which is perfectly acceptable, it seemed to be confusing you.

I hope this helps...

cotton.ma at 2007-7-8 8:42:03 > top of Java-index,Java Essentials,Java Programming...