Core Java Programming: From Beginner to Project Development

Lesson 2.4: Type Casting in Java

Introduction

In Java, different variables can store different types of data. Sometimes, we need to convert a value from one data type to another. This process is known as Type Casting.

For example, you may want to convert an integer into a decimal number, or a decimal number into an integer. Java provides two ways to perform these conversions:

  • Implicit Type Casting (Widening)

  • Explicit Type Casting (Narrowing)

Understanding type casting is important because it helps prevent data loss and allows different data types to work together in a program.


Learning Objectives

After completing this lesson, you will be able to:

  • Understand the concept of type casting.

  • Explain implicit (widening) type casting.

  • Explain explicit (narrowing) type casting.

  • Identify situations where type casting is required.

  • Write Java programs using type casting.


What is Type Casting?

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

Example:

int marks = 95;
double percentage = marks;

Here, the integer value 95 is automatically converted to 95.0.


Types of Type Casting

Java supports two types of type casting:

  1. Implicit Type Casting (Widening)

  2. Explicit Type Casting (Narrowing)


1. Implicit Type Casting (Widening)

Implicit type casting is performed automatically by Java.

It occurs when a smaller data type is converted into a larger data type.

Conversion Order

byte → short → int → long → float → double

Since there is no risk of losing data, Java performs this conversion automatically.


Example 1

public class WideningExample {

    public static void main(String[] args) {

        int number = 100;

        double value = number;

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

    }

}

Output

100
100.0

Example 2

float marks = 95;

double percentage = marks;

System.out.println(percentage);

Output

95.0

2. Explicit Type Casting (Narrowing)

Explicit type casting is performed manually by the programmer.

It occurs when a larger data type is converted into a smaller data type.

This conversion may result in loss of data, so Java requires an explicit cast.


Syntax

smallerDataType variable = (smallerDataType) value;

Example 1

public class NarrowingExample {

    public static void main(String[] args) {

        double percentage = 89.75;

        int marks = (int) percentage;

        System.out.println(percentage);
        System.out.println(marks);

    }

}

Output

89.75
89

Notice that the decimal part (.75) is removed.


Example 2

double price = 199.99;

int amount = (int) price;

System.out.println(amount);

Output

199

Type Casting with Characters

Characters can also be converted to integers using their Unicode values.

Example:

char letter = 'A';

int value = letter;

System.out.println(value);

Output

65

Another Example:

int number = 66;

char letter = (char) number;

System.out.println(letter);

Output

B

Real-World Example

Suppose a student’s average marks are stored as a decimal value:

double average = 89.8;

If the report card only accepts whole numbers:

int finalMarks = (int) average;

Output:

89

Data Loss in Narrowing Conversion

Example:

double salary = 45678.95;

int finalSalary = (int) salary;

System.out.println(finalSalary);

Output

45678

The decimal value is lost.


Conversion Summary

From To Automatic?
byte int Yes
int double Yes
float double Yes
double int No
long short No
double float No

Common Mistakes

Forgetting Explicit Casting

Incorrect:

double marks = 95.5;

int value = marks;

Compilation Error

Correct:

int value = (int) marks;

Expecting Rounded Values

double value = 9.99;

int number = (int) value;

Output:

9

Type casting does not round the value.

It simply removes the decimal part.


Best Practices

  • Use implicit casting whenever possible.

  • Use explicit casting only when necessary.

  • Be careful of data loss.

  • Choose appropriate data types to reduce unnecessary conversions.


Key Points to Remember

  • Type casting converts one data type into another.

  • Widening conversion is automatic.

  • Narrowing conversion requires explicit casting.

  • Narrowing may result in loss of data.

  • Java follows strict type safety rules.


Lesson Summary

In this lesson, you learned:

  • What type casting is.

  • The difference between widening and narrowing conversion.

  • How to perform implicit and explicit type casting.

  • How characters can be converted to integers and vice versa.

  • Common mistakes related to type casting.

Type casting is an important concept because it allows different data types to work together in Java programs.


Practice Questions

  1. What is type casting?

  2. What is the difference between widening and narrowing conversion?

  3. Which type of casting is automatic?

  4. Why is explicit casting required?

  5. Can data be lost during type casting?

  6. What is the Unicode value of the character 'A'?

  7. What happens when a double is converted to an int?

  8. Explain implicit and explicit type casting with examples.


Programming Exercise

Write Java programs to:

  1. Convert an int into a double.

  2. Convert a double into an int.

  3. Convert a char into its ASCII/Unicode value.

  4. Convert an integer into a character.

  5. Display the output of each conversion.


Assignment

Create a program named TypeCastingDemo.java that demonstrates:

  • One example of implicit type casting.

  • Two examples of explicit type casting.

  • One character-to-integer conversion.

  • One integer-to-character conversion.

Display the original values and the converted values with appropriate labels.

Next Lesson: Lesson 2.5 – Operators in Java

Scroll to Top