Skip to content
Related Articles

Related Articles

Improve Article

Arrays.binarySearch() in Java with examples | Set 1

  • Difficulty Level : Easy
  • Last Updated : 03 Jun, 2020
Geek Week

Arrays.binarySearch() is the simplest and most efficient method to find an element in a sorted array in Java


public static int binarySearch(data_type arr, data_type key )

where data_type can be any of the primitive data types: byte, char, double, int, float, short, long and Object as well.

Searches the specified array of the given data type for the specified value using the binary search algorithm. The array must be sorted (as by the Arrays.sort() method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.

arr – the array to be searched
key – the value to be searched for

index of the search key, if it is contained in the array; otherwise, (-(insertion point) – 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.


Searching for 35 in byteArr[] = {10,20,15,22,35}
will give result as 4 as it is the index of 35

Searching for g in charArr[] = {'g','p','q','c','i'}
will give result as 0 as it is the index of 'g'

Searching for 22 in intArr[] = {10,20,15,22,35};
will give result as 3 as it is the index of 22

Searching for 1.5 in doubleArr[] = {10.2,15.1,2.2,3.5}
will give result as -1 as it is the insertion point of 1.5

Searching for 35.0 in floatArr[] = {10.2f,15.1f,2.2f,3.5f}
will give result as -5 as it is the insertion point of 35.0

Searching for 5 in shortArr[] = {10,20,15,22,35}
will give result as -1 as it is the insertion point of 5

// Java program to demonstrate working of Arrays.
// binarySearch() in a sorted array.
import java.util.Arrays;
public class GFG
    public static void main(String[] args)
        byte byteArr[] = {10,20,15,22,35};
        char charArr[] = {'g','p','q','c','i'};
        int intArr[] = {10,20,15,22,35};
        double doubleArr[] = {10.2,15.1,2.2,3.5};
        float floatArr[] = {10.2f,15.1f,2.2f,3.5f};
        short shortArr[] = {10,20,15,22,35};
        byte byteKey = 35;
        char charKey = 'g';
        int intKey = 22;
        double doubleKey = 1.5;
        float floatKey = 35;
        short shortKey = 5;
        System.out.println(byteKey + " found at index = "
        System.out.println(charKey + " found at index = "
        System.out.println(intKey + " found at index = "
        System.out.println(doubleKey + " found at index = "
        System.out.println(floatKey + " found at index = "
        System.out.println(shortKey + " found at index = "


35 found at index = 4
g found at index = 1
22 found at index = 3
1.5 found at index = -1
35.0 found at index = -5
5 found at index = -1

Important Points:

  • If input list is not sorted, the results are undefined.
  • If there are duplicates, there is no guarantee which one will be found.

Arrays.binarysearch() vs Collections.binarysearch()
Arrays.binarysearch() works for arrays which can be of primitive data type also. Collections.binarysearch() works for objects Collections like ArrayList and LinkedList.

There are variants of this method in which we can also specify the range of array to search in. We will be discussing that as well as searching in an Object array in further posts.

This article is contributed by Shikhar. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :