WikiGalaxy

Personalize

Java Type Casting

Primitive Type Casting

In Java, primitive type casting is a process of converting one primitive type into another. It can be done in two ways: implicit and explicit casting.

Implicit Casting (Widening)

Implicit casting occurs when a smaller type is automatically converted to a larger type. For example, an int can be assigned to a long without explicit cast.


int num = 100;
long bigNum = num; // Implicit casting from int to long
System.out.println(bigNum); // Output: 100
    

Explicit Casting (Narrowing)

Explicit casting is required when converting a larger type to a smaller type. This is done by specifying the target type in parentheses before the value.


double decimal = 9.78;
int integer = (int) decimal; // Explicit casting from double to int
System.out.println(integer); // Output: 9
    

Object Type Casting

Upcasting

Upcasting is the type casting of a child object to a parent object. It is done automatically and is safe.


class Animal {}
class Dog extends Animal {}

Animal a = new Dog(); // Upcasting
    

Downcasting

Downcasting is the casting from a parent object to a child object. This requires explicit casting and can throw a ClassCastException if not done carefully.


Animal a = new Dog();
Dog d = (Dog) a; // Downcasting
    

Type Casting with Interfaces

Casting to Interface Type

When a class implements an interface, you can cast an instance of the class to the interface type.


interface Animal {}
class Dog implements Animal {}

Animal a = new Dog();
Dog d = (Dog) a; // Casting back to Dog
    

Casting Objects in Collections

Casting in Generics

When using generics, casting is often unnecessary, but it may be required when dealing with raw types.


List rawList = new ArrayList();
rawList.add("Hello");
String str = (String) rawList.get(0); // Casting required
    

Type Casting Exceptions

ClassCastException

A ClassCastException is thrown when an object is cast to a class of which it is not an instance.


Object obj = new Integer(100);
String str = (String) obj; // Throws ClassCastException
    

Type Casting in Arrays

Array Type Casting

You can cast arrays of one type to another, provided the types are compatible.


Number[] numbers = new Integer[10];
numbers[0] = 1; // Implicit casting of int to Integer
    

Type Casting with Enums

Casting Enums

Enums can be cast to their ordinal value or to a string representation.


enum Color { RED, GREEN, BLUE }

Color c = Color.RED;
int ordinal = c.ordinal(); // Cast to int
String name = c.name(); // Cast to String
    

Type Casting with Streams

Casting in Streams

In Java Streams, casting is often used when processing elements of a generic type or when converting to a specific type.


Stream mixedStream = Stream.of(1, "two", 3.0);
Stream stringStream = mixedStream
    .filter(e -> e instanceof String)
    .map(e -> (String) e);
    
  


Type Casting with Reflection

Reflection and Casting

Reflection in Java allows you to inspect classes, interfaces, fields, and methods at runtime, and casting is often necessary when working with reflection.


Class clazz = Class.forName("java.util.ArrayList");
Object obj = clazz.newInstance();
ArrayList list = (ArrayList) obj; // Casting required
    

Type Casting in Method Overloading

Casting for Method Calls

When overloading methods, casting may be necessary to call a specific method version.


void print(int num) { System.out.println("Integer: " + num); }
void print(double num) { System.out.println("Double: " + num); }

print((int) 5.5); // Calls the integer version
    
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025