binary search using recursion I implemented a binary search algorithm using recursion in Java
package com.soloworld.binarysearch;

/**
 *
 * @author soloworld
 */
public class BinarySearch {

   private int[] array;

    public BinarySearch(int[] array) {
        this.array = array;
    }

    public int recursivesearch(int[] array,int startindex,int endindex,int key) {
        if(endindex&gt;0) {
            int middle =(startindex+ endindex)/2;
            System.out.println("middle "+middle);
            if(array[middle]== key) return middle;
            else if (key &lt;array[middle]) {
                return recursivesearch(array, startindex, middle-1, key);
            } else if(key &gt; array[middle]) {

               return recursivesearch(array, middle+1, endindex, key);
            }
        }
        return -1;
    }


}


it worked, all i want to know is have  i done it in correct way or there any way to optimize this
It's always good to use guard conditions.  Instead of wrapping all of your logic in a code block, check for conditions when you don't have to execute and return early.  Like so:

public int recursivesearch(int[] array,int startindex,int endindex,int key) {
    if (endindex&lt;=0){
       return -1;
    }

    // everything else here
}


It's much easier to read if you structure your code like this: also much less error-prone.

Next, you are missing some guards.  What happens if you array is empty?  What happens if the value you are looking for is larger or smaller than any values in the array?  You have to check for these conditions and handle them individually.  You are also trusting that the user has passed a proper index in.

Also, I would make it easier to use by having the public method not require start and end index, calculating them from the length, and then passing it into the actual search method which is protected.

Finally, you were not properly detecting a missing value.  You were returning -1 if endIndex&lt;=0, but the proper condition is endIndex&lt;startIndex.  Yours ends in an infinite loop for items such as:

recusivesearch([1,2,3,4,6], 0, 4, 5)

Keep in mind that java is not my language of choice, so this might be best viewed as pseudo-code:

public int search(int[] array, int key) {
    if (array.length == 0){
        return -1;
    }

    if (key &lt; array[0] || key &gt; array[array.length-1]){
        return -1;
    }

    return this.recursiveSearch(array, key, 0, array.length-1);
}

protected int recursiveSearch(int[] array, int key, int startIndex, int endIndex){
    if (endIndex&lt;startIndex){
        return -1;
    }

    int middleIndex = startIndex + (endIndex-startIndex)/2;
    if (array[middleIndex] == key){
        return middleIndex;
    }
    if (key &lt; array[middleIndex]){
        return recursiveSearch(array, key, startIndex, middleIndex-1);
    }
    return recursiveSearch(array, key, middleIndex+1, endIndex);
}


Nitpicks

Your definition of middle was prone to integer overflow.  I adjusted the definition to fix that issue.

Watch out for your use of whitespace.  In the long run, consistent whitespace makes code maintenance much easier.  Java problem has some general standards, which I'm guessing at.

Same with capitalization.  I believe Java prefers camelCase.  Some people prefer snake_case.  Pick one and stick to it: you don't do either, and it makes it harder to read your variable names.

Naming variables is also important.  You have startindex and endindex, and then middle.  You should at least go for consistency:

startIndex, endIndex, middleIndex

Or

start, end, middle

Since these variables contain indexes, I would probably go for the former, but for shorter names I would also consider the latter acceptable.  Either way, Keep your names consistent.