Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return statment not working in Java

I have been trying a small code to learn about recursion in java. Wrote the below method to implement linear search using recursion. But when I call this method with an array and the variable to search as input and when the method reaches the return statement, it is not exiting the method. Instead, after executing the statements in the if loop, it's once again entering the else loop.

I have 2 questions.

1) Why is it not exiting the method on reaching 'return' ?

2) Why is it entering the else loop after executing the if loop ?

What am I doing wrong here ? Can someone please take a look and help me.

linearRecursiveSearch(Constants.INPUT_INT_ARRAY, Constants.INPUT_INT_ARRAY[0], Constants.NUMBER_TO_SEARCH); 



int count = 0;
public <T> void linearRecursiveSearch(T[] array,T tmp, T value) {
    count++;
    if (tmp == value) {
        System.out.println("The value has been found");
        return;
    } else {
        for (int i = count; i < array.length; i++) {
            T tmp1 = array[i];
            linearRecursiveSearch(array,tmp1, value);
        }
    }
}
like image 618
Indraneel Avatar asked Mar 14 '23 11:03

Indraneel


2 Answers

1) Why is it not exiting the method on reaching 'return' ?

It is exiting the method that invoked return.

2) Why is it entering the else loop after executing the if loop ?

The method that invoked return is not invoking the else loop. However there are other invocations of the method that are, and they are queued up to resume after the method that does invoke return completes.


The point of confusion is that there is more than one invocation of the method and they are not all calling return.

Perhaps it will help to recall what happens when a method invocation occurs, that is the state of the current method is pushed onto a stack. Space is then reserved on the stack for the new invocation and then that method is invoked. Once that method completes, its state is popped from the stack. Its return value is made available and the previous method that is now at the top of the stack is resumed.

During recursion, the same method may be pushed on to the stack hundreds of times. That means the method could be called hundreds of times, and then each method will be resumed hundreds of times as the stack unwinds. Thus just because the method that is currently being invoked calls return (and exits) it does not mean that all of the other queued up instances will too. In fact it actually means that the previous method (the one that called this method) will resume.


Consider the following version, notice that it does not contain a for loop or any global state:

public <T> int linearRecursiveSearch(T[] array, T targetValue) {
    return linearRecursiveSearch( array, targetValue, 0 );
}

private <T> int linearRecursiveSearch(T[] array, T targetValue, int i) {
    if ( i >= array.length ) {
        return -1;
    } else if (array[i] == targetValue) {  
        // == or .equals, or Comparator?  task for the reader
        return i;
    } else {
        return linearRecursiveSearch(array, targetValue,i+1);    
    }
}
like image 163
Chris K Avatar answered Mar 24 '23 18:03

Chris K


If I understand your problem this may be the solution

import groovy.util.logging.Slf4j
import org.junit.Test

@Slf4j
class MyTest {
@Test
public void myTest(){
    int count = 0;
    Integer[] input= [1,2,3,4,5,6,7]
    linearRecursiveSearch(input,5,0)
}

public <T> void linearRecursiveSearch(T[] array, T value,int count) {
    count++;
    if (array[count] == value) {
        System.out.println("The value has been found");
        return;
    } else {

            linearRecursiveSearch(array, value,count);

    }
}

}

like image 27
Filippo Fratoni Avatar answered Mar 24 '23 18:03

Filippo Fratoni