Definition of binarySearch
is as follows:
Searches the specified list for the specified object using the binary search algorithm.
static <T> int
binarySearch(List<? extends Comparable<? super T>> list, T key)
As you can see, list
and key
are parameterized on a type T
. Now Scala compiler tries to infer the type of list
in following invocation:
scala> java.util.Collections.binarySearch(l, 0)
<console>:9: error: type mismatch;
found : java.util.ArrayList[Integer]
required: java.util.List[_ <: Comparable[_ >: Any]]
java.util.Collections.binarySearch(l, 0)
And the common type between the parameters list
and key
is Any
. Because former is of type List<Integer>
and later is of type int
as per Java. So this converts in Scala to Integer
and int
respectively, as explained below:
scala> classOf[Integer]
res9: Class[Integer] = class java.lang.Integer
scala> classOf[Int]
res10: Class[Int] = int