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

ClassCastException when casting an Object[] to a String[]

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • tools
    • itanium
    • linux



      Name: js151677 Date: 08/30/2004


      FULL PRODUCT VERSION :
      [cheiny@venom castbug]$ java -version
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
      Java HotSpot(TM) 64-Bit Server VM (build 1.5.0-beta2-b51, mixed mode)
      [cheiny@venom castbug]$

      ADDITIONAL OS VERSION INFORMATION :
      [cheiny@venom castbug]$ uname -a
      Linux venom.synaptics.com 2.6.5-1.358 #1 Sat May 8 09:01:26 EDT 2004 x86_64 x86_64 x86_64 GNU/Linux
      [cheiny@venom castbug]$

      A DESCRIPTION OF THE PROBLEM :
      A ClassCastException occurs when casting the result of Set.toArray() to a String[]. As far as I can tell, this should work. The contents of the Set are Strings, I'm assigning it to a String[], and I'm casting the Object[] returned by toArray() as (String[]). Seems like it ought to work.

       This sort of handling does violate the
      "Principle of Least Surprise" in a big way - something that Java doesn't
      usually do. Unless there are situations where a cast such as this does not
      produce the ClassCastException, it would be much better for the compiler to
      flag this as an error, rather than waiting until runtime.

      As an RFE, it would be great if the toArray() utility functions for
      Collection<E> returned
          <E> E[]
      rather than
      Object[]
      That would certainly be very much in keeping with the new 1.5 (oops Java 5)
      improvements. Of course, it might not actually be possible without breaking
      a lot of code that depends on toArray returning Object[] - I don't know the
      answer to that.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and execute the program in the source code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It will run to completion without printing anything, like so:

      [cheiny@venom castbug]$ javac -source 1.5 CastBug.java
      [cheiny@venom castbug]$ java -cp . CastBug
      [cheiny@venom castbug]$
      ACTUAL -
      I get a ClassCastException as follows...

      [cheiny@venom castbug]$ javac -source 1.5 CastBug.java
      [cheiny@venom castbug]$ java -cp . CastBug
      Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
              at CastBug.main(CastBug.java:11)
      [cheiny@venom castbug]$

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      See Actual Result section.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.*;

      public class CastBug {

         public static void main ( String[] args ) {
              Set<String> mySet = new HashSet<String>();
              mySet.add("foo");
              mySet.add("bar");
              mySet.add("baz");
              mySet.add("bat");
              String[] myArray = (String[]) mySet.toArray();
         }

      }[
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :

          String[] myArray = new String[mySet.size()];
          int i = 0;
          for ( String s : mySet ) {
              myArray = s;
              i++;
          }
      (Incident Review ID: 297524)
      ======================================================================

            ahe Peter Ahe
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: