Java Type Casting – Complete Guide with Examples and Rules


Learn everything about Java type casting, including implicit (widening), explicit (narrowing) conversion, type promotion, and best practices with examples.

Type Casting in Java – Complete Detailed Tutorial

In Java, type casting is the process of converting a variable from one data type to another.

It allows compatibility between different data types in expressions, assignments, or method calls.

1. Why Type Casting is Needed

  1. To assign values of one type to another type
  2. To perform operations on variables of different types
  3. To prevent data loss or enable precision conversion

Example:


int a = 100;
double b = a; // int to double

2. Types of Type Casting in Java

Java supports two main types of type casting:

  1. Implicit Casting (Widening)
  2. Explicit Casting (Narrowing)

2.1 Implicit Casting (Widening Conversion)

  1. Automatic conversion by the compiler
  2. Converts smaller data type → larger data type
  3. Safe, no data loss
  4. Works for byte → short → int → long → float → double

Example:


int num = 50;
double d = num; // int → double automatically
System.out.println(d); // 50.0

Explanation:

  1. int can fit into double safely → no explicit cast required

Another Example:


byte b = 10;
int i = b; // byte → int
float f = i; // int → float
System.out.println(f); // 10.0

2.2 Explicit Casting (Narrowing Conversion)

  1. Manual conversion by the programmer
  2. Converts larger data type → smaller data type
  3. May lead to data loss
  4. Syntax: (targetType) value

Example:


double d = 99.99;
int i = (int) d; // double → int explicitly
System.out.println(i); // 99 (decimal part lost)

Another Example:


long l = 1000;
short s = (short) l; // long → short
System.out.println(s); // 1000

3. Type Casting Between Primitive Types

From → ToImplicitExplicit
byte → shortN/A
short → intN/A
int → longN/A
long → int(int)
float → doubleN/A
double → float(float)
char → intN/A
int → char(char)

4. Type Casting Between Reference Types

  1. Can cast objects of classes within the same hierarchy
  2. Use upcasting and downcasting

4.1 Upcasting (Implicit)

  1. Child → Parent
  2. Safe and automatic

class Animal {}
class Dog extends Animal {}

public class Test {
public static void main(String[] args) {
Dog d = new Dog();
Animal a = d; // upcasting
System.out.println("Upcasting done");
}
}

4.2 Downcasting (Explicit)

  1. Parent → Child
  2. Must be done explicitly, may throw ClassCastException

Animal a = new Dog();
Dog d = (Dog) a; // downcasting
System.out.println("Downcasting done");

5. Type Promotion in Expressions

  1. Smaller types are automatically promoted in expressions
  2. Example:

byte b1 = 10;
byte b2 = 20;
int sum = b1 + b2; // byte → int automatically
System.out.println(sum); // 30

6. Example Program – Type Casting


public class TypeCastingDemo {
public static void main(String[] args) {
// Implicit casting
int num = 100;
double d = num; // int → double
System.out.println("Implicit Casting: " + d);

// Explicit casting
double pi = 3.14159;
int intPi = (int) pi; // double → int
System.out.println("Explicit Casting: " + intPi);

// Expression type promotion
byte b1 = 10, b2 = 20;
int sum = b1 + b2;
System.out.println("Type Promotion in expression: " + sum);
}
}

Output:


Implicit Casting: 100.0
Explicit Casting: 3
Type Promotion in expression: 30

7. Summary

  1. Type casting converts variables from one type → another
  2. Implicit/Widening: smaller → larger, automatic, safe
  3. Explicit/Narrowing: larger → smaller, manual, may lose data
  4. Reference type casting: upcasting (safe), downcasting (requires explicit)
  5. Type promotion occurs in expressions to avoid data loss