DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java BasicsTopic 19 of 59
←PreviousPrevNextNext→

Java Type Casting - Complete Guide

Introduction to Type Casting

Type casting in Java is the process of converting one data type into another. It is useful when working with different numeric types or when handling objects in an inheritance hierarchy.

There are two main types of casting in Java: implicit (widening) and explicit (narrowing). Implicit casting is automatic and safe, while explicit casting requires care due to potential data loss or runtime errors.

Widening Casting (Implicit)

Widening casting occurs automatically when converting a smaller data type into a larger one (e.g., int to double). It is safe because no data will be lost.

Example
public class WideningCasting {
    public static void main(String[] args) {
        int myInt = 9;
        double myDouble = myInt; // Implicit cast: int to double

        System.out.println(myInt);    // 9
        System.out.println(myDouble); // 9.0

        // Other widening examples
        byte myByte = 10;
        short myShort = myByte;
        int myInt2 = myShort;
        long myLong = myInt2;
        float myFloat = myLong;
        double myDouble2 = myFloat;

        System.out.println("Byte: " + myByte);
        System.out.println("Short: " + myShort);
        System.out.println("Int: " + myInt2);
        System.out.println("Long: " + myLong);
        System.out.println("Float: " + myFloat);
        System.out.println("Double: " + myDouble2);
    }
}
Output
9
9.0
Byte: 10
Short: 10
Int: 10
Long: 10
Float: 10.0
Double: 10.0

Narrowing Casting (Explicit)

Narrowing casting must be done manually by placing the target type in parentheses before the value. It is used when converting a larger type into a smaller one and may lead to data loss or overflow.

Example
public class NarrowingCasting {
    public static void main(String[] args) {
        double myDouble = 9.78;
        int myInt = (int) myDouble; // Explicit cast: double to int

        System.out.println(myDouble); // 9.78
        System.out.println(myInt);    // 9 (decimal part lost)

        long myLong = 10000000000L;
        int myInt2 = (int) myLong; // Possible overflow
        float myFloat = 3.14f;
        short myShort = (short) myFloat;
        int myInt3 = 300;
        byte myByte = (byte) myInt3; // Possible overflow

        System.out.println("Long: " + myLong);
        System.out.println("Int from Long: " + myInt2);
        System.out.println("Float: " + myFloat);
        System.out.println("Short from Float: " + myShort);
        System.out.println("Int: " + myInt3);
        System.out.println("Byte from Int: " + myByte);
    }
}
Output
9.78
9
Long: 10000000000
Int from Long: 1410065408
Float: 3.14
Short from Float: 3
Int: 300
Byte from Int: 44

Type Casting with Objects

Objects can also be cast in an inheritance hierarchy. Upcasting (to a superclass) is implicit and safe, while downcasting (to a subclass) requires explicit casting and should be checked with `instanceof`.

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

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog barks");
    }

    public void fetch() {
        System.out.println("Dog fetches the ball");
    }
}

public class ObjectCasting {
    public static void main(String[] args) {
        Animal myAnimal = new Dog(); // Upcasting
        myAnimal.makeSound();

        if (myAnimal instanceof Dog) {
            Dog myDog = (Dog) myAnimal; // Downcasting
            myDog.fetch();
        }

        Animal anotherAnimal = new Animal();
        // Dog anotherDog = (Dog) anotherAnimal; // Would cause ClassCastException
    }
}
Output
Dog barks
Dog fetches the ball

Best Practices for Type Casting

  • ✅ Prefer widening casting since it is automatic and safe.
  • ✅ Be careful with narrowing casting due to possible precision loss or overflow.
  • ✅ Always use `instanceof` before downcasting objects.
  • ✅ Use wrapper classes (e.g., `Integer.parseInt`) for safe type conversions.
  • ✅ Avoid unnecessary casts as they make code harder to read.
  • ✅ Use parentheses for clarity when combining casting with other operations.
  • ✅ Document explicit casts when they are required for clarity.
  • ✅ Test carefully when casting between numeric types to avoid unexpected results.
Test your knowledge: Java Type Casting - Complete Guide
Quiz Configuration
4 of 10 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java BasicsTopic 19 of 59
←PreviousPrevNextNext→