Java Type Casting

Welcome to The Coding College! In this tutorial, we’ll dive into Java Type Casting, its types, and how you can use it effectively in your Java programs. Type casting allows you to convert a variable from one data type to another.

What Is Type Casting in Java?

Type casting is the process of converting one data type into another. It can occur in two forms:

  1. Implicit Casting (Widening): Automatic conversion performed by Java.
  2. Explicit Casting (Narrowing): Manual conversion by the programmer.

1. Implicit Type Casting (Widening)

  • Occurs when a smaller data type is converted to a larger data type.
  • Done automatically by Java as there is no risk of data loss.
  • Examples:
    • byte → short → int → long → float → double

Example of Widening:

public class ImplicitCasting {
    public static void main(String[] args) {
        int intValue = 100;
        double doubleValue = intValue; // Automatically converts int to double
        System.out.println("Integer value: " + intValue);
        System.out.println("Double value: " + doubleValue);
    }
}

Output:

Integer value: 100  
Double value: 100.0

2. Explicit Type Casting (Narrowing)

  • Occurs when a larger data type is converted into a smaller data type.
  • Done manually using parentheses (dataType).
  • May cause data loss or precision loss.

Example of Narrowing:

public class ExplicitCasting {
    public static void main(String[] args) {
        double doubleValue = 99.99;
        int intValue = (int) doubleValue; // Manually casting double to int
        System.out.println("Double value: " + doubleValue);
        System.out.println("Integer value after casting: " + intValue);
    }
}

Output:

Double value: 99.99  
Integer value after casting: 99

Type Casting and Data Loss

Example of Data Loss:

public class DataLossExample {
    public static void main(String[] args) {
        int largeNumber = 130;
        byte smallNumber = (byte) largeNumber; // Casting int to byte
        System.out.println("Original number: " + largeNumber);
        System.out.println("After casting to byte: " + smallNumber);
    }
}

Output:

Original number: 130  
After casting to byte: -126

Explanation:
The value 130 exceeds the range of byte (-128 to 127), resulting in overflow and incorrect representation.

Type Casting Between Non-Primitive Types

In Java, you can cast objects of related classes using:

  • Upcasting: Casting a subclass to a superclass (done implicitly).
  • Downcasting: Casting a superclass to a subclass (done explicitly).

Example:

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

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

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

        Dog dog = (Dog) animal; // Downcasting
        dog.sound();
    }
}

Output:

Dog barks  
Dog barks

Practice Exercises

  1. Create a program that converts an int to a float using implicit casting and displays both values.
  2. Write a program to demonstrate explicit casting from a double to an int and show the impact on data precision.
  3. Create a superclass Vehicle and a subclass Car, then demonstrate upcasting and downcasting between the two.

Key Points to Remember

  • Use implicit casting when moving from smaller to larger data types.
  • Perform explicit casting carefully to avoid data loss or precision issues.
  • Casting between object types requires a clear relationship (inheritance).

Conclusion

Understanding Java Type Casting is essential for working with different data types and objects effectively. Practice these concepts to build a strong foundation.

For more tutorials like this, visit The Coding College and continue enhancing your coding skills. 🚀

Leave a Comment