Explicit and Implicit Type Casting

This section describes type casting supported in Java: up casting (widening reference conversion) and down casting (narrowing reference conversion). Cast operation can be written explicitly with the cast operator (T), or implicitly with no operator.

In Java, class and interface reference type can be converted from one type to another type using the cast operation in two ways:

1. Widening Reference Conversion - Type S is converted to type T, where S is a subtype of T. Widening reference conversion is also called up casting, because it converts a subtype to a supertype. Up casting is always allowed, because the reference object of a subtype is always compatible with a supertype. For example:

    String msg = new String("Hello");

    // up casting from String to Object
    Object obj = (Object) msg;

2. Narrowing Reference Conversion - Type T is converted to type S, where T is a supertype of S. Narrowing reference conversion is also called down casting, because it converts a supertype to a subtype. Down casting is always allowed only if the reference object is compatible with the subtype. If the reference object is not compatible with the subtype, a compilation error or runtime exception will be resulted. For example:

    Object obj = new String("Hello");

    // down casting from Object to String
    String msg = (String) obj;

There are 2 syntax formats to write a type casting operation:

1. Explicit Casting - Adding (cast-to type) on the left side of the cast-from type. For example,

    String msg = new String("Hello");

    // explicit casting
    Object obj = (Object) msg;

2. Implicit Casting - Letting compiler automatically cast the type based on expression context. For example,

    String msg = new String("Hello");

    // explicit casting
    Object obj = msg;

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 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

 Enum Types and Enum Constants

 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

 Java Modules - Java Package Aggregation

 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

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB