Skip to content

Java Type casting

Hey there, If you've ever wondered how to convert data from one type to another seamlessly, you're in for a treat. So, buckle up and get ready to embark on this exciting journey with us!

Type casting is like a magic trick in the world of programming. It allows us to transform data from one type to another, opening up a world of possibilities in our code. Whether it's converting a number to a different data type or casting objects between classes, type casting is a powerful tool in the Java program

Types of Type Casting

Type casting in Java is like adjusting the shape of a puzzle piece to fit into a different spot. There are two main ways to do it: implicit and explicit.

Implicit Type Casting (Widening):

Implicit type casting, also known as widening, occurs when you convert a data type with smaller size into a data type with larger size. Java does this automatically, without any explicit instruction from you. // Syntax: larger_data_type variableName = smaller_data_type_variable; long numLong = numInt; // Implicit type casting from int to long Let's look at an example:

java
int numInt = 100;
long numLong = numInt; // Implicit type casting from int to long

In this example, the int value is implicitly casted to a long value because long can hold larger values than int.

Explicit Type Casting (Narrowing):

Explicit type casting, on the other hand, is a manual process where you explicitly specify the desired data type. This is necessary when you want to convert a data type with larger size into a data type with smaller size, as it may result in data loss. Let's see how it works:

// Syntax: data_type targetVariable = (target_data_type) source_variable;
int numInt = (int) numDouble; // Explicit type casting from double to int
java
double numDouble = 3.14;
int numInt = (int) numDouble; // Explicit type casting from double to int

Here, we're explicitly casting the double value to an int value. Keep in mind that this may lead to loss of precision, as decimals are truncated.

Example

java
public class TypeCastingExample {
    public static void main(String[] args) {
        // Implicit Type Casting (Widening)
        int numInt = 100;
        long numLong = numInt; // Implicit type casting from int to long
        
        System.out.println("Implicit Type Casting (Widening):");
        System.out.println("int value: " + numInt);
        System.out.println("long value: " + numLong);
        
        // Explicit Type Casting (Narrowing)
        double numDouble = 3.14;
        int numIntAgain = (int) numDouble; // Explicit type casting from double to int
        
        System.out.println("\nExplicit Type Casting (Narrowing):");
        System.out.println("double value: " + numDouble);
        System.out.println("int value (after casting): " + numIntAgain);
    }
}

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.