Ugenz Academy/Tutorial

Typecasting

7 min read1,220 words

Reading progress

Keep learning

Type Casting in Java

Introduction

Type casting is the process of converting a value from one data type to another data type.

For example, converting an int value into a double value is called type casting.

int number = 10; double value = number; System.out.println(value);

Output:

10.0

Java type casting is mainly divided into two types:

Type Casting │ ├── Widening Casting │ └── Narrowing Casting

1. Widening Casting

Widening casting means converting a smaller data type into a larger compatible data type.

It is also called implicit casting because Java usually performs it automatically.

Conversion

byte ↓ short ↓ int ↓ long ↓ float ↓ double

Example

public class Main { public static void main(String[] args) { int number = 100; double value = number; System.out.println(number); System.out.println(value); } }

Output:

100 100.0

Here:

int → double

No explicit casting is required.

2. Narrowing Casting

Narrowing casting means converting a larger data type into a smaller compatible data type.

It is also called explicit casting.

Because data may be lost, we must specify the target type manually.

Syntax

dataType variable = (dataType) value;

Example

public class Main { public static void main(String[] args) { double number = 10.75; int value = (int) number; System.out.println(number); System.out.println(value); } }

Output:

10.75 10

The decimal part .75 is discarded.

3. Difference Between Widening and Narrowing

Widening CastingNarrowing Casting
Smaller type → larger typeLarger type → smaller type
Usually automaticMust be explicit
Also called implicit castingAlso called explicit casting
Usually no numeric information is lostData may be lost
int → doubledouble → int

4. Widening Casting Example

public class Main { public static void main(String[] args) { int marks = 85; double percentage = marks; System.out.println("Marks: " + marks); System.out.println("Percentage: " + percentage); } }

Output:

Marks: 85 Percentage: 85.0

5. Narrowing Casting Example

public class Main { public static void main(String[] args) { double price = 500.75; int newPrice = (int) price; System.out.println("Original Price: " + price); System.out.println("Converted Price: " + newPrice); } }

Output:

Original Price: 500.75 Converted Price: 500

6. Type Casting Between Integer Types

Type casting can also be performed between integer types.

public class Main { public static void main(String[] args) { long number = 1000L; int value = (int) number; System.out.println(value); } }

Output:

1000

Here:

long → int

requires explicit casting.

7. Character Type Casting

A char can be converted to an integer type because Java represents characters using numeric code units.

public class Main { public static void main(String[] args) { char letter = 'A'; int number = letter; System.out.println(number); } }

Output:

65

Here:

char → int

is widening conversion.

8. Integer to Character

An integer can be explicitly converted to char.

public class Main { public static void main(String[] args) { int number = 65; char letter = (char) number; System.out.println(letter); } }

Output:

A

Here:

int → char

is explicit narrowing conversion.

9. Arithmetic and Type Casting

Type casting can be useful when performing calculations.

For example:

public class Main { public static void main(String[] args) { int a = 10; int b = 3; double result = (double) a / b; System.out.println(result); } }

Output:

3.3333333333333335

Without casting:

int result = a / b;

The result would be:

3

because integer division discards the fractional part.

10. Type Casting with Variables

public class Main { public static void main(String[] args) { int age = 20; double newAge = age; System.out.println("Integer: " + age); System.out.println("Double: " + newAge); } }

Output:

Integer: 20 Double: 20.0

11. Data Loss in Narrowing Casting

Narrowing conversion can cause loss of information.

Example:

public class Main { public static void main(String[] args) { double number = 99.99; int value = (int) number; System.out.println(value); } }

Output:

99

The .99 is discarded.

Important: Casting a floating-point value to an integer truncates the fractional part; it does not round to the nearest integer.

12. Overflow During Narrowing

Narrowing can also produce unexpected values if the original value is outside the range of the target type.

Example:

public class Main { public static void main(String[] args) { int number = 130; byte value = (byte) number; System.out.println(value); } }

Output:

-126

This happens because 130 is outside the range of byte, which is:

-128 to 127

13. Type Casting vs Type Conversion

These terms are often used together, but there is a useful distinction:

Type Conversion

A value is converted from one type to another, often automatically when Java permits it.

int number = 10; double value = number;

Type Casting

The programmer explicitly specifies the target type.

double number = 10.5; int value = (int) number;

14. Object Type Casting

Type casting can also be used with objects in an inheritance hierarchy.

For example:

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

Upcasting

A child object can be assigned to a parent reference.

Animal a = new Dog();

This is called upcasting and is generally automatic.

Downcasting

A parent reference can be explicitly cast to a child reference when the object actually refers to that child type.

Animal a = new Dog(); Dog d = (Dog) a; d.bark();

Output:

Dog barks

Downcasting should be done carefully because an invalid cast can cause ClassCastException.

15. Complete Example

public class Main { public static void main(String[] args) { // Widening int number = 100; double wide = number; // Narrowing double price = 99.99; int narrow = (int) price; // Character to integer char grade = 'A'; int code = grade; // Integer to character int value = 66; char letter = (char) value; System.out.println("Widening: " + wide); System.out.println("Narrowing: " + narrow); System.out.println("Character code: " + code); System.out.println("Character: " + letter); } }

Output:

Widening: 100.0 Narrowing: 99 Character code: 65 Character: B

Summary

Type Casting │ ├── Widening Casting │ ├── Automatic │ ├── Smaller → Larger │ └── Example: int → double │ └── Narrowing Casting ├── Explicit ├── Larger → Smaller └── Example: double → int

Key Syntax

Widening

double value = 10;

Narrowing

int value = (int) 10.5;

Conclusion

Type casting allows Java programs to convert values between compatible data types. Widening casting is generally automatic and moves from a smaller numeric type to a larger one, while narrowing casting requires explicit syntax and may result in data loss or overflow. Understanding type casting is important when working with calculations, variables, and object-oriented programming.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy