Paramclasses Academy - Blog

Tutorials Details

Type Casting in Java: Widening and Narrowing Explained

Type Casting in Java: Widening and Narrowing Explained

October 21, 2026

Introduction

Type casting is an important concept in Java programming. It allows a value of one data type to be converted into another compatible data type.

For example, you may need to convert an integer into a decimal number or convert a decimal number into an integer. Java supports these conversions through widening and narrowing conversions.

In this tutorial, you will learn how type casting works, when to use it, and how to avoid common mistakes.

What Is Type Casting in Java?

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

Consider this example:

int number = 10;
double result = number;

System.out.println(result);

Output:

10.0

Here, the integer value 10 is converted to the double value 10.0.

Java performs this conversion automatically because converting an int to a double is a widening primitive conversion.

There are two main categories of primitive numeric conversion:

  1. Widening casting

  2. Narrowing casting

1. Widening Casting in Java

Widening casting converts a value from one primitive numeric type to another type that can represent a wider range or greater precision in the relevant sense.

This conversion is generally performed automatically by Java, so it is also called implicit casting.

A common widening path is:

byte → short → int → long → float → double

char can also widen to int, long, float or double.

Example of Widening Casting

public class Main {
    public static void main(String[] args) {
        int number = 100;
        double result = number;

        System.out.println("Integer: " + number);
        System.out.println("Double: " + result);
    }
}

Output:

Integer: 100
Double: 100.0

How Does It Work?

  • number is an int variable containing 100.

  • result is a double variable.

  • Java converts the integer value to a double automatically.

  • The output displays 100.0.

More Widening Examples

byte a = 10;
int b = a;

int number = 25;
double decimal = number;

char letter = 'A';
int characterCode = letter;

System.out.println(b);
System.out.println(decimal);
System.out.println(characterCode);

Output:

10
25.0
65

The character 'A' has the numeric UTF-16 code unit value 65.

Important: Widening is not guaranteed to preserve every value exactly. For example, converting a very large long to float can lose integer precision, and converting long to float or double may round the value.

2. Narrowing Casting in Java

Narrowing casting converts a value to a type that may represent a smaller range or less precision. Java usually requires an explicit cast because information may be lost.

Syntax

targetType variableName = (targetType) value;

Example of Narrowing Casting

public class Main {
    public static void main(String[] args) {
        double number = 9.75;
        int result = (int) number;

        System.out.println("Double: " + number);
        System.out.println("Integer: " + result);
    }
}

Output:

Double: 9.75
Integer: 9

How Does It Work?

  • number contains the double value 9.75.

  • (int) explicitly converts the value to an integer.

  • The fractional part is discarded, so result becomes 9.

Java does not round 9.75 to 10 in this cast; it truncates the fractional part toward zero.

More Narrowing Examples

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

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

System.out.println(wholePrice);
System.out.println(smallNumber);

Output:

99
-126

The first conversion discards the decimal fraction. The second conversion produces -126 because narrowing an integer to a byte keeps the low eight bits and interprets them as a signed byte.

This demonstrates why narrowing conversions require care.

Difference Between Widening and Narrowing Casting

Widening Casting Narrowing Casting
Converts to a wider numeric type Converts to a potentially narrower numeric type
Usually happens automatically Usually requires an explicit cast
Often preserves the value May lose precision or change the value
Example: int to double Example: double to int
Example: int x = 10; double y = x; Example: double x = 9.8; int y = (int) x;

Implicit vs Explicit Casting

Implicit casting occurs automatically when Java allows the conversion without an explicit cast.

int number = 50;
double result = number;

Explicit casting uses the target type in parentheses to request a conversion.

double number = 50.75;
int result = (int) number;

In the second example, the result is 50.

Can We Convert a String to an Integer?

Converting a String to an int is not primitive numeric casting. It is parsing: Java reads text and attempts to interpret it as an integer.

Use Integer.parseInt() for this purpose.

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

System.out.println(number + 10);

Output:

133

If the string does not contain a valid integer representation, Integer.parseInt() throws a NumberFormatException.

For example:

String text = "hello";
// int number = Integer.parseInt(text); // Throws NumberFormatException

Complete Practical Java Program

This program demonstrates widening, narrowing and parsing.

public class Main {
    public static void main(String[] args) {
        // Widening
        int number = 25;
        double wideNumber = number;

        // Narrowing
        double price = 99.75;
        int wholePrice = (int) price;

        // Parsing a String
        String text = "100";
        int parsedNumber = Integer.parseInt(text);

        System.out.println("Widening: " + wideNumber);
        System.out.println("Narrowing: " + wholePrice);
        System.out.println("Parsed value: " + parsedNumber);
    }
}

Output:

Widening: 25.0
Narrowing: 99
Parsed value: 100

Common Mistakes Beginners Should Avoid

  1. Forgetting the explicit cast: Assigning a double directly to an int causes a compile-time error.

  2. Expecting rounding: (int) 9.99 produces 9, not 10.

  3. Ignoring data loss: Narrowing a large number can produce an unexpected value.

  4. Confusing parsing with casting: Integer.parseInt("123") parses text; it is not primitive numeric casting.

  5. Assuming widening always preserves precision: Conversions to float or double can lose precision for some integer values.

  6. Using invalid text in parsing: A non-numeric string can cause NumberFormatException.

Frequently Asked Questions (FAQs)

1. What is type casting in Java?

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

2. What is widening casting?

Widening is a primitive numeric conversion that moves to a wider type, such as converting int to double. It usually happens automatically.

3. What is narrowing casting?

Narrowing converts a value to a type that may have a smaller range or less precision. It usually requires an explicit cast.

4. Does narrowing casting round decimal values?

No. Casting a floating-point value to an integer discards the fractional part toward zero.

5. What is implicit casting?

Implicit casting is a conversion Java performs automatically when the conversion is allowed without an explicit cast.

6. What is explicit casting?

Explicit casting uses syntax such as (int) number to request a conversion.

7. How do you convert a String to an integer?

Use Integer.parseInt() when the string contains a valid integer representation.

Practice Exercises

Try these exercises:

  1. Convert an int value to double and print the result.

  2. Convert a double value to int using explicit casting.

  3. Convert the char value 'B' to an integer and print its numeric value.

  4. Convert the string "250" to an integer using Integer.parseInt().

  5. Try converting 130 to byte and observe the output.

  6. Explain why (int) 8.95 produces 8.

Challenge: Write a program that stores a product price as a double, converts it to an int, and prints both values. Explain why the results differ.

Conclusion

Type casting helps Java programmers convert values between compatible data types. Widening conversions are usually automatic, while narrowing conversions generally require an explicit cast and can lose information.

Practise these examples to understand the differences and use type conversions correctly in real-world programs.