JDK-8151591 : Object can be cast to primitive
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u66
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2016-02-29
  • Updated: 2016-03-10
  • Resolved: 2016-03-10
Related Reports
Duplicate :  
Description
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 ----------


Comments
Test result: ######### OS: Windows 7 64-bit JAVAC: ********** 6uXX : Fail 7uXX : Fail 8uXX : Fail 9ea : Pass Netbeans: ************* 6uXX : Fail 7uXX : Fail 8uXX : Fail 9ea : Fail ======================================================================= Output: ======================================================================= 6uXX: ##### c:\ji\tools>javac -version javac 1.6.0_45 c:\ji\tools>javac Test.java Test.java:5: inconvertible types found : java.lang.Object required: boolean boolean bool = (boolean) o; ^ Test.java:11: inconvertible types found : T required: boolean boolean bool = (boolean) t; ^ 2 errors ------------------------------------------------------------------------------------------------------------------- 7uXX: ##### c:\ji\tools>javac -version javac 1.7.0_51 c:\ji\tools>javac Test.java Test.java:11: error: inconvertible types boolean bool = (boolean) t; ^ required: boolean found: T where T is a type-variable: T extends Object declared in class Test.TestInner 1 error ------------------------------------------------------------------------------------------------------------------- 8uXX: ##### c:\ji\tools>javac -version javac 1.8.0 c:\ji\tools>javac Test.java Test.java:11: 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 ------------------------------------------------------------------------------------------------------------------- 9ea: #### c:\ji\tools>javac -version javac 9-ea c:\ji\tools>javac Test.java c:\ji\tools>
10-03-2016