Type Casting Compile and Runtime Error

This section provides a tutorial example on compilation time casting error and runtime casting exception.

If you are trying to convert reference type to another type that is not compatible, you will get a compiler error. Here is a simple example:

/* TypeCastingCompileError.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
class TypeCastingCompileError {
   public static void main(String[] a) {

      // Compiler error: Casting from Integer to Long is not allowed
      Integer myInteger = new Integer(777);
      Long myLong = (Long) myInteger;
   }
}

Try to compile it with JDK 1.8:

TypeCastingCompileError.java:11: error: incompatible types: 
   Integer cannot be converted to Long
      Long myLong = (Long) myInteger;
                           ^
1 error

If you want, you can avoid the compiler error by casting Integer to Object, before casting it to Long. But you will get a runtime error. Here is a simple example:

/* TypeCastingRuntimeError.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
class TypeCastingRuntimeError {
   public static void main(String[] a) {

      try {
         // Runtime error: Casting from Integer to Long is not allowed
         Integer myInteger = new Integer(777);
         Object myObject = (Object) myInteger;
         Long myLong = (Long) myObject;
      } catch (Exception e) {
         e.printStackTrace();
      }
   
      try {
         // Runtime error: Casting from Integer to Long is not allowed
         Integer hisInteger = new Integer(888);
         Number hisNumber = (Number) hisInteger;
         Long hisLong = (Long) hisNumber;
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Try to compile and run it with JDK 1.8:

java.lang.ClassCastException: 
   java.lang.Integer cannot be cast to java.lang.Long
        at TypeCastingRuntimeError.main(TypeCastingRuntimeError.java:12)

java.lang.ClassCastException: 
   java.lang.Integer cannot be cast to java.lang.Long
        at TypeCastingRuntimeError.main(TypeCastingRuntimeError.java:21)

As you can see from this example, the compiler fails to detect the type casting error, because down casting "(Long) Object" or "(Long) Number" is a valid casting if the reference object is a Long object.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

Reference Data Types and Variables

 Reference Types Supported in Java

 Creating Class Type Objects

 Class Type Variables Storing References

 Interface Type Variables

 Class and Interface Hierarchy

 Supertype and Subtype

 Explicit and Implicit Type Casting

 Type Casting Example Program

Type Casting Compile and Runtime Error

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version