Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4483464

Arrays.binarySearch() does not throw ClassCastException

XMLWordPrintable

    • sparc
    • solaris_2.6



      Name: auR10023 Date: 07/24/2001



      java.util.Arrays.binarySearch(Object[], Object) does not throw
      ClassCastException with array containing elements wich are not comparable with
      the search key. The javadoc says:

      public static int binarySearch(Object[] a,
                                     Object key)

      ...

      Throws:
                ClassCastException - if the search key in not comparable to the elements
                of the array.
      ...

      Here is the example:

      --------------t.java-----------
      import java.util.*;

      public class t {
          public static void main(String [] args) {
              Object[] arrayOfObj= new Object[3];
              arrayOfObj[0]="Test";
              arrayOfObj[1]= new Integer(1);
              arrayOfObj[2]="String";
              Integer key = new Integer(1);
              try{
                  Arrays.binarySearch(arrayOfObj, key);
              }
              catch(ClassCastException e){
                  System.out.println("ClassCastException was thrown");
                  return;
              }
              System.out.println("ClassCastException wasn't thrown");
          }
      }

      -----output:------
      #java -version

      java version "1.4.0-beta_refresh"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta_refresh-b69)
      Java HotSpot(TM) Client VM (build 1.4.0-beta_refresh-b69, mixed mode)

      #java t

      ClassCastException wasn't thrown

      ======================================================================

            jjb Josh Bloch
            avusunw Avu Avu (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: