Lesson 12 of 50 – Type Casting in Java
24%

Type Casting in Java

Type casting is the process of converting a value from one data type to another. Java supports automatic conversions for certain compatible types and explicit conversions using a cast operator.

Learning Objective: Understand widening and narrowing conversions, casting syntax, examples, and common type-casting errors in Java.
1. What Is Type Casting?

Type casting converts a value from one data type to another data type.

double price = 99.75;
int amount = (int) price;

System.out.println(amount);

Output:

99

The cast (int) converts the double value to an integer. The fractional part is discarded, not rounded.

2. Types of Type Casting

There are two common categories of primitive numeric conversion:

Type Meaning Example
Widening Converts a type to a wider compatible type automatically. int to double
Narrowing Converts a type to a narrower type using an explicit cast. double to int

Widening generally preserves the numeric value, though converting a very large integer to a floating-point type may lose precision. Narrowing can lose information.

3. Widening Type Casting

Widening conversion changes a value to a compatible type that can represent a broader range of values. It usually happens automatically.

int number = 100;
double result = number;

System.out.println(number);
System.out.println(result);

Output:

100
100.0

Java automatically converts the integer value into a double value.

4. Widening Conversion Order

The following diagram shows common widening primitive conversion paths:

byte → short → int → long → float → double
char ─────────→ int → long → float → double

A char can widen to an integer type because it represents a UTF-16 code unit.

char letter = 'A';
int code = letter;

System.out.println(code);

Output:

65
This diagram shows common conversion paths, not every possible conversion rule. For example, char can widen to int, but a short cannot be assigned directly to a char without an explicit cast.
5. Narrowing Type Casting

Narrowing conversion changes a value to a type that has a smaller range or less precision. An explicit cast is generally required for primitive numeric narrowing conversions.

double price = 125.85;
int wholePrice = (int) price;

System.out.println(wholePrice);

Output:

125

The decimal portion is removed when converting this positive floating-point value to an integer.

6. Type Casting Between int and double

You can convert an int to a double automatically or cast a double to an int explicitly.

int a = 50;
double b = a;

double x = 45.90;
int y = (int) x;

System.out.println(b);
System.out.println(y);

Output:

50.0
45

Remember that converting a floating-point value to an integer truncates the fractional part toward zero.

7. Type Casting Between int and char

An integer can be explicitly cast to a char, and a char can widen to an integer.

int number = 65;
char letter = (char) number;

System.out.println(letter);

char ch = 'B';
int code = ch;

System.out.println(code);

Output:

A
66

The number 65 corresponds to the character A, and the character B has the numeric code-unit value 66.

8. Type Casting Between int and byte

A byte stores values from -128 to 127. Casting an integer outside this range can change the resulting value.

int number = 130;
byte result = (byte) number;

System.out.println(result);

Output:

-126

The result follows the narrowing conversion rules for the 8-bit signed byte type. The original value cannot be represented as a byte.

Do not assume that casting to a smaller integer type checks whether the value fits within its range.
9. Type Casting in Calculations

Casting can change how arithmetic expressions are evaluated.

int a = 5;
int b = 2;

System.out.println(a / b);
System.out.println((double) a / b);

Output:

2
2.5

In the first expression, integer division discards the fractional part. In the second, casting a to double causes floating-point division.

10. Casting Does Not Convert Strings Automatically

A string containing digits cannot be converted to an integer using a primitive cast.

Incorrect approach:

String text = "100";
// int number = (int) text; // Compilation error

Correct approach:

String text = "100";
int number = Integer.parseInt(text);

System.out.println(number + 20);

Output:

120

Use methods such as Integer.parseInt() to parse numeric text. Invalid input can cause a NumberFormatException.

11. Common Type Casting Errors
Problem Explanation or Solution
Assigning double to int Use an explicit cast when truncation is intended.
Unexpected decimal loss Converting a floating-point value to an integer discards its fractional part.
Integer overflow after casting Check the target type's range before narrowing.
Casting String to int Use Integer.parseInt() for valid integer text.
Unexpected integer division Convert an operand to a floating-point type before division.
12. Practical Example: Student Marks

Suppose a student's marks are stored as a decimal value. You can cast them to an integer when you intentionally want to discard the fractional part.

public class StudentMarks {
    public static void main(String[] args) {
        double marks = 85.75;
        int wholeMarks = (int) marks;

        System.out.println("Original Marks: " + marks);
        System.out.println("Whole Marks: " + wholeMarks);
    }
}

Output:

Original Marks: 85.75
Whole Marks: 85

If you need rounding rather than truncation, use an appropriate rounding method instead of a simple cast.

13. Practice Program and Summary

Write a program that converts a double to an integer and an integer to a double.

public class TypeCastingDemo {
    public static void main(String[] args) {
        int number = 25;
        double decimalNumber = number;

        double price = 99.99;
        int wholePrice = (int) price;

        System.out.println("Integer: " + number);
        System.out.println("Double: " + decimalNumber);
        System.out.println("Original Price: " + price);
        System.out.println("Casted Price: " + wholePrice);
    }
}

Output:

Integer: 25
Double: 25.0
Original Price: 99.99
Casted Price: 99

Key Points:

  • Type casting converts a value from one type to another.
  • Widening conversions are usually automatic.
  • Narrowing primitive numeric conversions generally require an explicit cast.
  • Casting floating-point values to integers truncates the fractional part.
  • Casting to a smaller integer type can lose information.
  • Use parsing methods to convert numeric strings.
Congratulations! You have learned the basics of type casting in Java.

🧠 Quick Quiz

1. Which conversion usually happens automatically?




2. What is the output of this code?

double price = 45.90;
int amount = (int) price;
System.out.println(amount);



3. Which method converts valid integer text to an int?