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

Object can be cast to primitive

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When a reference of type "Object" is cast directly to the primitive type "boolean", javac compiler accepts this without errors.
      However, if the reference has the type of a type-variable (which should become "Object" during compilation), the cast is not possible.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See description

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Both casts should produce a compiler error.
      ACTUAL -
      C:\Users\zith\Documents\Test.java:9: error: incompatible types: T cannot be converted to boolean
                              boolean bool = (boolean) t;
                                                       ^
        where T is a type-variable:
          T extends Object declared in class Test.TestInner
      1 error

      This error message is only produced if "t" is of the generic type "T", but not if "t" is of type "Object" itself.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {
      public static void main (String[] args) {
      Object o = new Object();
                      // cast from Object to boolean, works without error
        boolean bool = (boolean) o;
      }

      class TestInner<T> {
      public void doCast(T t) {
                              // cast from T to boolean, produces a compiler error
        boolean bool = (boolean) t;
        }
      }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: