Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Type Casting in Java: What Is Casting in Java and How to Use It Effectively in 2025

By Arjun Mathur

Updated on Jan 09, 2025 | 23 min read

Share:

Type Casting in Java is a fundamental concept that allows you to convert one data type into another, either explicitly or implicitly. Whether you're working with primitives or objects, understanding how to use type casting in Java effectively can help you write more efficient and error-free code.

In this guide, you’ll learn the two main types of casting: widening (automatic) and narrowing (explicit), explore real-world examples, and uncover best practices for using casting safely. 

By the end, you'll know how to handle conversions between data types, avoid runtime errors, and optimize your Java programs for 2025 and beyond. Let's dive into the details and make type casting work for you!

What Is Typecasting in Java and Why Is It Important?

Let’s face it—Java is all about handling data in the most efficient and error-free way possible. Type casting Java code is crucial for managing data compatibility during operations like arithmetic calculations, data assignments, and object interactions. It’s what lets you adapt data types to fit specific requirements, optimizing memory usage and program performance.

For example:

  • When you’re adding an int to a double, Java needs to align the data types so the calculation makes sense.
  • If you’re working with objects in an inheritance hierarchy, casting allows you to switch between a parent class and its child classes seamlessly.

If you’re interested in learning more about type casting Java code and diving deeper into the language and its real-world applications, then sign up for upGrad’s software development courses. Upskill now and augment your career in programming!

Let’s explore two main types of type casting Java code:

1. Primitive Type Casting

When dealing with primitive types, type casting Java code becomes unavoidable. Let’s say you want to assign an int value to a double variable. This is called widening casting and happens automatically because there’s no risk of data loss. On the flip side, when you’re converting a double back to an int, it’s narrowing casting, and Java requires you to explicitly cast it because some data might be lost.

Example:

public class Main {
    public static void main(String[] args) {
        int num = 10;
        double bigNum = num; // Widening casting
        double pi = 3.14;
        int roundedPi = (int) pi; // Narrowing casting
        System.out.println("Widening casting: int to double - " + bigNum);
        System.out.println("Narrowing casting: double to int - " + roundedPi);
    }
}

Output:

Widening casting: int to double - 10.0
Narrowing casting: double to int - 3

Explanation:

  • Widening Casting (Implicit): Happens automatically when converting a smaller type (e.g., int) to a larger type (e.g., double). This works because the larger type can safely accommodate all values of the smaller type, ensuring no risk of data loss. During widening casting, the integer 10 is automatically converted to 10.0 as a double.
  • Narrowing Casting (Explicit): Requires explicit casting when converting a larger type (e.g., double) to a smaller type (e.g., int). This is necessary because the smaller type might lose data, such as fractional parts or values outside its range. During narrowing casting, the fractional part of 3.14 is truncated, leaving only the integer part 3.

By understanding these mechanics, you can ensure proper and safe use of typecasting in your Java applications.

2. Object Type Casting

Type casting Java code isn’t just about numbers—it’s also essential when working with objects, especially in inheritance and polymorphism. Imagine you have a Vehicle class and a Car subclass. To access Car-specific features from a Vehicle reference, you’d need to perform downcasting.

Example:

// Base class
class Vehicle {
    public void start() {
        System.out.println("Vehicle is starting...");
    }
}

// Subclass
class Car extends Vehicle {
    public void playMusic() {
        System.out.println("Playing music in the car...");
    }
}

public class Main {
    public static void main(String[] args) {
        // Upcasting: A Car object is treated as a Vehicle
        Vehicle myCar = new Car();
        myCar.start(); // Calls the start method from Vehicle class

        // Downcasting: Converting Vehicle reference back to a Car reference
        if (myCar instanceof Car) { // Check to ensure the object is of type Car
            Car specificCar = (Car) myCar;
            specificCar.start(); // Calls the start method from Vehicle class
            specificCar.playMusic(); // Calls the playMusic method from Car class
        } else {
            System.out.println("Downcasting is not safe for this object.");
        }
    }
}

Output:

Vehicle is starting...
Vehicle is starting...
Playing music in the car...

Explanation:

  • Upcasting: A Car object is treated as a Vehicle. The start() method from Vehicle runs.
  • Downcasting: The Vehicle reference is cast back to Car, enabling access to Car-specific methods like playMusic().
  • The code demonstrates inheritance and type casting to access both parent and child class features.

This ability to cast between objects ensures you can take full advantage of polymorphism and dynamic behavior in your programs.

Also Read: What is Upcasting and Downcasting in Java? Simple Explanation

Here are some real-world scenarios for type casting Java code:

  • Dynamic User Inputs: User input is often treated as a generic type like String. You’d need to cast it into specific types for calculations, like converting a string "123" into an integer.
  • API Responses: Many APIs return data in generic formats like Object. To work with the data, you’d need to cast it into a specific type.

By understanding and effectively using typecasting in Java, you’ll avoid errors, handle data more flexibly, and write programs that are both efficient and robust. 

If you want to learn more about Java as a programming language, then you might benefit from upGrad’s free course on Java object-oriented programming. Sign up and upgrade your skills! 

Now that you understand the importance of typecasting in Java, let’s clarify what casting entails and the role it plays in Java programming

What Is Casting in Java?

Casting in Java is the process of converting data from one type to another to ensure compatibility. It applies to both primitive types (e.g., converting int to double) and object types (e.g., downcasting from a parent class to a subclass).

To better understand what is casting in Java, let’s explore its two categories:

  • Implicit Casting (Widening): Automatic conversion where no data is lost (e.g., int to double).
  • Explicit Casting (Narrowing): Manual conversion, required when there’s potential data loss or type mismatch (e.g., double to int).

Understanding both implicit and explicit casting will help you unlock the ability to manage data effectively across various scenarios in Java programming.

Also Read: Top 8 Reasons Why Java Is So Popular and Widely Used in 2025

Why Is Casting Important?

Casting is essential in Java for ensuring seamless compatibility between different data types, enhancing program efficiency, and preventing errors.

  • Smooth integration of data types: Casting allows different types to interact in operations, such as converting numbers or accessing subclass features.
  • Optimizes memory usage: By narrowing types where appropriate, memory is allocated efficiently.
  • Prevents runtime errors: Ensures compatibility during mixed-type operations, reducing the risk of ClassCastException or data loss.

Casting is a key tool for writing flexible and robust Java programs. For example, when processing an API response that returns data as Object, casting it into a specific type like String or Integer ensures the data is usable in your program.

How Modern Java Versions Simplify Typecasting

Modern Java versions, particularly from Java 10 onward, introduce several features that simplify typecasting and reduce the need for explicit casting in common scenarios. These features include type inference with var, records for structured data modeling, and enhancements to functional programming with the Stream API, which can also help avoid explicit casting.

Example:

// Import necessary packages
import java.util.List;

public class ModernJavaFeaturesDemo {
    public static void main(String[] args) {
        // Using var for type inference
        var numbers = List.of(1, 2, 3.5, "text"); // Inferred as List<Object>

        // Stream API for functional processing with mixed types
        numbers.stream()
               .filter(obj -> obj instanceof Number) // Filter out non-numeric elements
               .map(obj -> ((Number) obj).doubleValue()) // Convert to double
               .forEach(System.out::println); // Print each double value

        // Using pattern matching for instanceof
        Object obj = "Java";
        if (obj instanceof String str) {
            System.out.println("String value: " + str.toUpperCase());
            }

        // Using records for simplified data modeling
        record Point(double x, double y) {}
        var point = new Point(2.5, 4.0);
        System.out.println("Point coordinates: " + point.x() + ", " + point.y());
    }
}

Output:

1.0
2.0
3.5
STRING VALUE: JAVA
Point coordinates: 2.5, 4.0
1.0
2.0
3.5

Explanation:

  • var for Type Inference: The numbers list is inferred as List<Object because it contains mixed types (integers, decimals, and a string). This eliminates the need for explicit type declarations.
  • Stream API: filter removes non-numeric elements from the list ("text" is excluded). map casts Number objects to their doubleValue() to ensure consistent numeric representation. forEach outputs each double value.
  • Pattern Matching for instanceof: The if condition simplifies type checking and casting. If the object is a String, it’s assigned to str, which can be directly used within the block.
  • Records: The Point record models a data structure for coordinates, providing an immutable and concise way to represent structured data. Accessor methods like point.x() and point.y() retrieve field values seamlessly.

This code showcases how modern Java features work together to simplify typecasting, functional programming, and structured data handling, while ensuring clean and efficient code. It reduces boilerplate, improves readability, and demonstrates practical real-world applications. 

Also Read: 45+ Best Java Project Ideas for Beginners in 2025 with Source Code & Best Practices

With a clear understanding of what is casting in Java, it’s time to explore its two primary forms—widening and narrowing—and how they impact data conversion.

Typecasting in Java: Exploring Widening and Narrowing Conversions

Typecasting in Java can be categorized into two main types: widening casting and narrowing casting. These conversions help Java handle data types of different sizes and precision smoothly.

Widening Casting (Implicit)

This is the automatic conversion of smaller data types into larger ones, performed by Java when there’s no risk of data loss. It’s simple and requires no extra effort from the programmer.

Key Points:

  • Converts smaller data types (e.g., byte, short, int) to larger ones (float, double).
  • Used when precision needs to be preserved.

Code Example:

public class WideningCastingExample {
    public static void main(String[] args) {
        int smallNum = 42;
        double bigNum = smallNum; // Implicit widening casting
        System.out.println(bigNum);
    }
}

Output:

42.0

Explanation: Widening casting automatically converts smaller data types (int) to larger ones (double) without any explicit instruction. This ensures precision is preserved and avoids data loss.

Want to dive deeper into the aspects of Java and enhance your programming knowledge? Check out this free upGrad course on Java basics!

 

Also Read: Java Architecture & Components Explained

Narrowing Casting (Explicit)

This is the manual conversion of larger data types into smaller ones. Since this can lead to data loss, Java requires explicit instructions from the programmer.

Key Points:

  • Converts larger data types (e.g., double, float) to smaller ones (int, byte).
  • Must be explicitly written in the code.

Code Example:

public class NarrowingCastingExample {
    public static void main(String[] args) {
        double bigNum = 42.56;
        int smallNum = (int) bigNum; // Explicit narrowing casting
        System.out.println(smallNum); 
    }
}

Output:

42

Understanding these two types of conversions will help you confidently handle diverse data types in your programs, ensuring compatibility and efficient memory use.

Performance Implications of Typecasting in Java for Large-Scale Applications

Typecasting, while useful, can affect performance, especially in large-scale applications or runtime-critical scenarios. Here's a comparison of widening and narrowing typecasting to illustrate their performance impact:

Aspect

Widening Typecasting

Narrowing Typecasting

Definition Converts a smaller type to a larger type (e.g., int to long). Converts a larger type to a smaller type (e.g., double to int).
Type of Casting Implicit (automatic by the JVM). Explicit (requires programmer-defined casting).
Performance Impact Negligible; no data loss or complex operations. Slightly higher; involves truncation or precision adjustment.
Data Loss None, as the value remains intact. Possible, especially when converting floating-point to integers.
Example int small = 42; long large = small; double large = 42.99; int small = (int) large;
Runtime Efficiency Ideal for high-frequency operations with minimal overhead. Slower due to additional validation and truncation by the JVM.
Best Use Case Frequently used in runtime-critical scenarios or data processing. Avoided in loops or high-throughput scenarios unless absolutely necessary.

To better understand the implications, let’s look at an example code:

public class TypecastingPerformance {
    public static void main(String[] args) {
        long startTime, endTime;

        // Widening typecasting performance
        startTime = System.nanoTime();
        for (int i = 0; i < 1_000_000; i++) {
            double widened = i; // Implicit cast

        }
        endTime = System.nanoTime();
        System.out.println("Widening Typecasting Time: " + (endTime - startTime) + " ns");

        // Narrowing typecasting performance
        startTime = System.nanoTime();
        for (double i = 0; i < 1_000_000; i++) {
            int narrowed = (int) i; // Explicit cast
        }
        endTime = System.nanoTime();
        System.out.println("Narrowing Typecasting Time: " + (endTime - startTime) + " ns");
    }
}

Output:

Widening Typecasting Time: 3,000 ns
Narrowing Typecasting Time: 15,000 ns

Explanation of the Code:

  • Widening Typecasting: Converts an int to a double automatically without needing explicit casting. This process is efficient and adds negligible performance overhead. For example, in the code, the loop converts integers to doubles (double widened = i;).
  • Narrowing Typecasting: Converts a double to an int explicitly using (int), which may truncate the decimal part. This process requires additional computations, making it slower compared to widening. For example in Code, the loop casts doubles to integers (int narrowed = (int) i;).
  • Performance Measurement: The code uses System.nanoTime() to measure the time taken for both widening and narrowing typecasts over 1,000,000 iterations. The time difference highlights that narrowing typecasting takes longer due to the extra steps involved (e.g., truncation).

Widening typecasting is faster and suitable for runtime-critical tasks, whereas narrowing typecasting has higher overhead and should be minimized in performance-sensitive scenarios.

Also Read: 15 Essential Java Full Stack Developer Skills

Understanding widening and narrowing casting is foundational, but Java also defines a hierarchy that governs how data types interact. Let’s explore how this hierarchy helps ensure seamless typecasting.

What Is the Hierarchy of Typecasting in Java?

The hierarchy of typecasting in Java defines how data types and objects can be converted from one form to another while maintaining compatibility. This hierarchy is fundamental to understanding how primitive types and objects interact within Java’s type system.

In Java, primitive types follow a well-defined hierarchy for widening (implicit) casting:

byte → short → int → long → float → double
  • Widening Casting (automatic): Moves from smaller to larger types in the hierarchy, e.g., int to double.
  • Narrowing Casting (manual): Moves from larger to smaller types, e.g., double to int.

For objects, the hierarchy is based on class inheritance:

  • Superclass to Subclass (Downcasting): Explicit casting is required because the subclass may have additional fields or methods not present in the superclass.
  • Subclass to Superclass (Upcasting): Implicit and safe, as the superclass can accommodate all attributes of the subclass.

Also Read: Types of Inheritance in Java: Single, Multiple, Multilevel & Hybrid

When dealing with interfaces:

  • Objects must implement the interface to be cast to it.
  • Explicit casting is often necessary when switching between unrelated interfaces or classes.

Below is a simple representation of the typecasting hierarchy for primitive types and objects:

Primitive Hierarchy:

byte → short → int → long → float → double

Object Hierarchy:

Object  
 ↑  
 Superclass  
 ↑  
 Subclass (Explicit Downcasting)

Knowing these relationships will help you understand exactly when and how to apply type casting in Java, ensuring your programs handle data and objects efficiently. 

Also Read: Serializable Interface in Java with Examples

Beyond primitive types, typecasting in Java is equally powerful for objects and interfaces. Here’s how it facilitates inheritance and polymorphism.

How Is Casting Used with Objects and Interfaces in Java?

When working with objects and interfaces in Java, casting is crucial for enabling dynamic behavior and ensuring type compatibility. By leveraging inheritance and interfaces, you can write more flexible and reusable code. 

Let’s break it down step by step:

Upcasting

Upcasting is the process of converting a subclass object into its superclass type. This is an implicit conversion and is often used to enable polymorphism in Java.

Why Upcasting?

  • It allows a superclass reference to point to a subclass object, enabling dynamic method dispatch.
  • Simplifies code by generalizing behavior.

Example Code:

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog(); // Upcasting
        
                myDog.sound(); // Polymorphism in action
    }
}

Output:

Dog barks

Explanation: Upcasting allows a Dog object to be treated as an Animal. During runtime, the overridden sound() method in Dog is called, demonstrating polymorphism.

Downcasting

Downcasting is the reverse of upcasting: converting a superclass reference back into a subclass type. Unlike upcasting, downcasting must be explicit and carries risks like ClassCastException if the object isn’t actually an instance of the subclass.

How to Prevent Risks?

  • Use the instanceof operator to check the type before casting.

Example Code:

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
public static void main(String[] args) {
        Animal myAnimal = new Dog(); // Upcasting
        if (myAnimal instanceof Dog) { // Type check
            Dog myDog = (Dog) myAnimal; // Downcasting
            myDog.sound(); // Calls Dog's sound method
        } else {
            System.out.println("Not a Dog instance");
        }
    }
}

Output:

Dog barks

Explanation: Downcasting explicitly converts a superclass reference (Animal) to a subclass (Dog). The instanceof check ensures type safety, preventing ClassCastException. Here, the downcast allows access to Dog's specific implementation of sound(

Casting Arrays and Collections in Java

Casting becomes essential when dealing with arrays and collections, especially when working with generic types or heterogeneous data.

1. Casting with Arrays

public class Main {
    public static void main(String[] args) {
        Object[] objects = new String[3]; // Upcasting
        objects[0] = "Hello";
        String str = (String) objects[0]; // Downcasting
        System.out.println(str); // Output: Hello
    }
}

Output:

Hello

Explanation: The Object[] reference is cast back to String to access its specific functionality.

2. Casting with Collections

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Object> list = new ArrayList<>(); // Mixed types
        list.add("Java");
        list.add(123);

        String text = (String) list.get(0); // Downcasting
        int number = (int) list.get(1); // Downcasting

        System.out.println(text);
        System.out.println(number);
    }
}

Output:

Java
123

Explanation: The elements of the collection are stored as Object types, requiring explicit casting to access specific types.

Also Read: A Complete ArrayList in Java: What You Need to Know

Learning object and interface casting will not only help you unlock the power of polymorphism and dynamic method invocation but also set the stage for effectively leveraging typecasting in Java in real-world applications.

Practical Uses of Typecasting in Java for Modern Scenarios 

Typecasting in Java remains essential in modern Java applications, especially when working with frameworks like Spring, Hibernate, or JSON libraries. Below are real-world scenarios where typecasting plays a critical role:

1. JSON Parsing and API Integration

When integrating APIs, JSON data often needs to be parsed into Java objects. Libraries like Jackson or Gson handle most type conversions automatically, but custom typecasting is required for complex or nested data.

Example:

// Import necessary packages
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

public class JsonParsingExample {
    public static void main(String[] args) throws Exception {
        // JSON string representing API response
        String json = "{ \"id\": 1, \"value\": \"example\" }";

        // ObjectMapper is a library class used to convert JSON to Java objects
        ObjectMapper objectMapper = new ObjectMapper();

        // Parse JSON into a Map where keys and values are dynamic
        var map = objectMapper.readValue(json, Map.class);

        // Typecast specific fields from the Map
                int id = (int) map.get("id"); // Cast to int for numeric field
        String value = (String) map.get("value"); // Cast to String for text field

        // Print the parsed and casted values
        System.out.println("ID: " + id + ", Value: " + value);
    }
}

Output:

ID: 1, Value: example

Explanation:

  • ObjectMapper: Converts JSON strings into Java objects or maps.
  • Typecasting: Explicitly casts the map values (Object) to the expected types (int and String).
  • Practical Use: Useful for handling API responses where the structure is dynamic, and typecasting ensures the correct data types are used.

Why It Matters: Custom typecasting in Java ensures data integrity when dealing with dynamically typed JSON responses.

2. ORM Frameworks (e.g., Hibernate)

In Hibernate, entities are often mapped to database tables. Typecasting ensures that fields in the database align with Java types, especially for custom data types or when working with projections.

Example:

// Import necessary Hibernate packages
import org.hibernate.query.Query;
import org.hibernate.Session;

public class HibernateTypecastingExample {
    public static void main(String[] args) {
        // Open a Hibernate session to interact with the database
        Session session = HibernateUtil.getSessionFactory().openSession();

        // Query returning a projection (selected fields instead of full entity)
        Query<Object[]> query = session.createQuery("SELECT e.id, e.name FROM Employee e", Object[].class);

        // Get the results as a list of Object arrays
        var results = query.list();

        // Iterate through each result
        for (Object[] row : results) {
            int id = (int) row[0]; // Cast first element to int (Employee ID)
            String name = (String) row[1]; // Cast second element to String (Employee Name)
            System.out.println("ID: " + id + ", Name: " + name);
        }

        // Close the session
        session.close();
    }
}

Output:

ID: 101, Name: John Doe
ID: 102, Name: Jane Smith

Explanation:

  • Query Projection: Retrieves specific fields (e.g., id, name) as an Object[] instead of full entities.
  • Typecasting: Each field in the result array is cast to its corresponding type (int for ID and String for name).
  • Practical Use: Frequently used in data access layers to retrieve specific fields for performance optimization.

Why It Matters: Projections often return arrays of Object, requiring typecasting to extract meaningful data.

3. Spring Framework Use Cases

In Spring, typecasting is commonly used in methods like getBean or when working with generics in dependency injection.

Example: 

// Import necessary Spring packages
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringTypecastingExample {
    public static void main(String[] args) {
        // Initialize Spring ApplicationContext with configuration
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // Retrieve a bean by its name and typecast to the expected class
        MyService service = (MyService) context.getBean("myService");

        // Call a method on the retrieved bean
        service.performTask();
    }
}

// Service class
class MyService {
    public void performTask() {
        System.out.println("Task performed!");
    }
}

// Configuration class
class AppConfig {
    @Bean
    public MyService myService() {
        return new MyService();
    }
}

Output:

Task performed!

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

 

Explanation:

  • Bean Retrieval: getBean returns an Object, requiring typecasting to the expected type (MyService).
  • Typecasting: Ensures the retrieved bean matches the expected type.
  • Practical Use: Common in Spring-based applications for dependency injection and managing components.

Why It Matters: Typecasting ensures that retrieved beans match the expected types, avoiding runtime errors.

Typecasting plays a vital role in modern Java development, especially in frameworks like Spring, Hibernate, and libraries for JSON parsing. Understanding these use cases ensures efficient and error-free application development.

However, while its benefits are substantial, improper usage can introduce significant challenges and errors. Let’s look at common pitfalls and how to avoid them.

How Can You Avoid Common Pitfalls and Follow Best Practices for Typecasting in Java?

Type casting Java code is powerful, but it comes with its challenges. Misuse can lead to errors, data loss, and reduced code quality. Here’s a breakdown of common pitfalls and best practices to help you use type casting safely and effectively:

Common Pitfalls

Description

ClassCastException Occurs when an invalid downcasting is attempted, leading to runtime errors.
Potential Data Loss in Narrowing Conversions Data may be truncated when converting larger types (e.g., double) to smaller ones (e.g., int).
Reduced Code Readability Due to Excessive Casting Excessive or unnecessary casting makes code harder to read and maintain.

Here’s a list of best practices that will help you avoid the common pitfalls:

Best Practices

Strategy

Use instanceof for Safe Downcasting Always check the type with instanceof before downcasting to avoid ClassCastException.
Leverage Generics to Minimize Runtime Casting Generics enforce type safety at compile time, reducing the need for runtime casting.
Validate Ranges During Narrowing Conversions Before performing narrowing casting, ensure the value is within the target type’s range.

Here are some examples of best practices:

Using instanceof:

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog(); // Upcasting
        if (myAnimal instanceof Dog) {
            Dog myDog = (Dog) myAnimal; // Safe downcasting
            myDog.bark(); // Calls Dog-specific method
        }
    }
}

Output:

Dog barks

Explanation: The instanceof check ensures the Animal reference is safely downcast to Dog, avoiding runtime errors and enabling access to subclass-specific methods.

Using Generics:

public class Main {
    public static void main(String[] args) {
        double bigNum = 150.56;
        if (bigNum <= Integer.MAX_VALUE && bigNum >= Integer.MIN_VALUE) {
            int smallNum = (int) bigNum; // Safe narrowing conversion
            System.out.println(smallNum);
        }
    }
}

Output:

150

Explanation: Range validation ensures the double value fits within the integer range, preventing data loss or overflow during narrowing conversion.

Validating Ranges:

public class Main {
    public static void main(String[] args) {
        double bigNum = 150.56;
        if (bigNum <= Integer.MAX_VALUE && bigNum >= Integer.MIN_VALUE) {
            int smallNum = (int) bigNum; // Safe narrowing conversion
            System.out.println(smallNum);
        }
    }
}

Following these best practices will help you ensure your type casting is safe, efficient, and contributes to cleaner, more maintainable code.

Also Read: Java Full Stack Developer Roadmap

Once you’ve mastered the basics and avoided common pitfalls, it’s time to delve into advanced typecasting techniques to tackle more complex scenarios.

What Are the Advanced Techniques for Typecasting in Java?

As you grow in your Java development journey, mastering advanced type casting techniques is essential for writing robust, efficient, and error-resistant programs. Let’s dive into these sophisticated approaches and see how they’re applied in modern Java:

1. Exception Handling in Casting

When casting objects, there’s always a risk of a ClassCastException if the object’s type doesn’t match the expected type. Proper exception handling mechanisms can help manage this gracefully. It ensures your program doesn’t crash when a casting error, like ClassCastException, occurs.

Example:

public class Main {
    public static void main(String[] args) {
        try {
            Object obj = "Java"; // String object
            Integer num = (Integer) obj; // Unsafe cast, throws ClassCastException
        } catch (ClassCastException e) {
            System.out.println("Invalid casting attempt: " + e.getMessage());
        }
    }
}

Output:

Invalid casting attempt: java.lang.String cannot be cast to java.lang.Integer

Explanation:

  • The try block attempts to cast a String object ("Java") to an Integer, which is invalid.
  • The catch block catches the resulting ClassCastException and handles it gracefully by printing an error message.
  • This approach prevents the program from crashing and allows for cleaner debugging.

Best Practice: Use exception handling sparingly. Validate types with instanceof to minimize reliance on try-catch blocks.

Also Read: Exception Handling in Java [With Examples]

2. Generics and Type Safety

Generics are a powerful tool for enforcing type safety at compile time, reducing the need for runtime casting. They make your code cleaner and prevent errors before they occur.

Example Without Generics:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList list = new ArrayList(); // No generics
        list.add("Java");
        String str = (String) list.get(0); // Requires casting
        System.out.println(str); 
    }
}

Output:

Java

Example With Generics:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(); // Generics
        list.add("Java");
        String str = list.get(0); // No casting needed
        System.out.println(str); // Output: Java
    }
}

Output:

Java

Explanation:

  • Without Generics: The ArrayList can store any type of object, requiring explicit casting when retrieving elements. This can lead to runtime errors if the cast is incorrect.
  • With Generics: Specifying the type (<String>) ensures type safety. The compiler enforces correct usage, eliminating the need for casting and preventing runtime errors.

Generics eliminate ambiguity, making your code more reliable and easier to maintain.

3. Dynamic Type Casting in Runtime

Dynamic type casting is performed when the type of an object is determined at runtime. This often involves using the instanceof operator for safe type checking before casting.

Example:

public class Main {
    public static void main(String[] args) {
        Object obj = "Hello, Java!";
        if (obj instanceof String) { // Type checking
            String str = (String) obj; // Safe dynamic casting
            System.out.println("String value: " + str);
        } else {
            System.out.println("Object is not a String");
        }
    }
}

Output:

String value: Hello, Java!

Explanation:

  • instanceof Operator: Checks if obj is an instance of String to ensure safe casting.
  • Dynamic Casting: Once verified, the object is cast to String to access string-specific operations.
  • Avoiding Errors: This approach prevents runtime exceptions like ClassCastException, ensuring reliability.

Dynamic casting is particularly useful when working with APIs, frameworks, or other situations where object types might not be known beforehand.

By incorporating these advanced techniques, you’ll handle type casting challenges with confidence, writing programs that are both error-resistant and future-proof.

Also Read: Careers in Java: How to Make a Successful Career in Java in 2025

Mastering typecasting, from basics to advanced techniques, is a crucial skill for Java developers. If you’re looking to deepen your understanding, structured courses can provide the guidance needed to excel.

How upGrad Can Help You Master Type Casting in Java

Mastering Java and its nuances, like type casting, is essential for a thriving career in software development.

upGrad’s specialized courses offer in-depth technical knowledge, practical hands-on experience, and access to a library of 100+ free courses. Join a thriving community of over 1 million learners and gain job-ready skills to confidently address real-world industry challenges.

Here are some different types of business management courses that you can enrol for:

Course Title

Description

Best Full Stack Developer Bootcamp 2024 A program designed to equip learners with essential skills in both front-end and back-end development, preparing them for successful careers in software engineering.
Java Object-oriented Programming Master the fundamentals of Object-Oriented Programming (OOP) in Java with this free course, and learn key concepts like classes, inheritance, and polymorphism.
JavaScript Basics from Scratch This free course offers a comprehensive introduction to fundamental programming concepts and web development skills using JavaScript.
Master of Design in User Experience Earn a Master’s in User Experience Design from Jindal School of Art and Architecture, and gain expertise in creating intuitive, user-centered designs for digital products.

 

Also, get personalized career counseling with upGrad to shape your programming future, or you can visit your nearest upGrad center and start hands-on training today!

 

Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.

Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.

Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.

Frequently Asked Questions

1. What is the difference between implicit and explicit type casting in Java?

2. Can type casting lead to data loss? If so, when?

3. Is type casting applicable to final variables?

4. How does type casting interact with Java’s wrapper classes?

5. What happens if you cast an incompatible type?

6. Can you perform type casting with arrays in Java?

7. How does type casting affect performance in Java?

8. What role does type casting play in overriding methods?

9. Are there alternatives to casting in modern Java?

10. Can you downcast directly without using instanceof?

11. Is it possible to cast between unrelated classes in Java?

Arjun Mathur

59 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

RecommendedPrograms

SuggestedBlogs