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

java VM doesn't cast an array of references according to casting conversion

    XMLWordPrintable

Details

    • sparc
    • solaris_2.5.1

    Description



      Name: dkC59003 Date: 07/08/98



      Compiler version "1.2beta4" (build JDK-1.2beta4-K, green threads, sunwjit)

      JLS, 5.5, p.69-70 says:
      "If R is a class representing an array type RC[]-that is, an array of components of type RC:

      If T is a class type, then T must be Object, or a run-time exception is thrown.

              If T is an interface type, then a run-time exception is thrown unless T is the interface
      type Cloneable, the only interface implemented by arrays (this case could slip past the
      compile-time checking if, for example, a reference to an array were stored in a variable
      of type Object).
       
      If T is an array type TC[], that is, an array of components of type TC, then a run-time
      exception is thrown unless one of the following is true:
                      TC and RC are the same primitive type.

                      TC and RC are reference types and type RC can be cast
      to TC by a recursive application of these run-time rules for casting." ( !!! )

      Java VM does not allow cast from array where elements are one reference type (here Object, TC in JLS),
      to array where elements are its subclass type (here String, RC in JLS).

      In the example below java VM should not throw an exception according
      to JLS while trying to cast "Object[] o" to "String[] s", because type
      of all objects of an array are the same type as type in a cast expression.

      An example and VM diagnostics follow:
      -------------------------------------------------------
      public class conv08305 {
             public static void main(String argv[]) {

                boolean b1 = true;
                String s[] = {null,null};
                Object o[] = {"1","2"};
                try {
                   s = (String[]) o; //narrowing conversion
                }catch(ClassCastException e) {
                   b1 = false;
                }
                if ( b1 ) System.out.println("Match JLS"); /*STATUS_PASSED*/
                System.out.println("b1=" + b1);
                /*STATUS_FAILED*/
             }
      }

      > uname -a
      SunOS novo35 5.5.1 Generic_103640-12 sun4m sparc SUNW,SPARCstation-20
      > javac conv08305.java
      > java conv08305
      b1=false
      >
      -------------------------------------------------------

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

      Attachments

        Activity

          People

            sliangsunw Sheng Liang (Inactive)
            dkhukhrosunw Dmitry Khukhro (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: