Type Casting in Java with Example: A Complete Guide

Robert Jul 13, 2026

Type casting in Java is a process by which one type of data type is upgraded to another or one type of data is decoded into another. This allows for more flexibility and efficient programming while preventing type mismatch errors.

Java Type Promotion, Type Casting & Conversion Notes
Java Type Promotion, Type Casting & Conversion Notes

Java is a statically-typed language, hence data types are checked at compile-time. However, programmers might need to explicitly convert the value of one data type to another, which is when type casting is employed.

Java Type Conversion & Type Casting Notes
Java Type Conversion & Type Casting Notes

Implicit Type Casting in Java

Sometimes, Java automatically performs type conversion, known as implicit type casting if one primitive data type can be converted into another losslessly. This includes converting from a smaller size data type to a larger size data type.

Java Type Casting Cheat Sheet 💻🔥 | Beginner Friendly
Java Type Casting Cheat Sheet 💻🔥 | Beginner Friendly

However, this is limited only to compatible types, and not all types are compatible. For instance, it's not valid to implicitly convert a double to an int.

Examples of Implicit Type Casting

online help
online help

For instance, the following code will compile and run successfully:

int x = 10;
double y = x;

Here, the value of the integer x is implicitly converted to a double and assigned to y.

Type Conversion and Casting in Java - Scientech Easy
Type Conversion and Casting in Java - Scientech Easy

However, if we try to assign the double y to an int like so:

int z = y;

We'll get a compile-time error because Java doesn't implicitly convert from a larger type to a smaller type. This is where explicit type casting comes into play.

Java Variables Explained in 60 Seconds | Java Programming Basics
Java Variables Explained in 60 Seconds | Java Programming Basics

Explicit Type Casting in Java

Explicit type casting is used to convert a value of one data type to another when the two types are not compatible for implicit type casting. This is done using a colon (:) followed by the new type name. This is known as casting.

Constructor in Java Explained | Default & Parameterized Constructor
Constructor in Java Explained | Default & Parameterized Constructor
What is type casting in Java?
What is type casting in Java?
Basic Data Types in Java with Example | Java Primitive and Non-Primitive Data Types with Syntax - BTech Geeks
Basic Data Types in Java with Example | Java Primitive and Non-Primitive Data Types with Syntax - BTech Geeks
Java Type Conversion with various examples
Java Type Conversion with various examples
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
an image of a computer screen with the words if else statement on it and two blocks of code
an image of a computer screen with the words if else statement on it and two blocks of code
Abstraction in Java - Learn with its Types and Real-life Examples - TechVidvan
Abstraction in Java - Learn with its Types and Real-life Examples - TechVidvan
Java Cursor Enumeration vs Iterator vs ListIterator
Java Cursor Enumeration vs Iterator vs ListIterator
Java Type Conversion - Upgrade your skills with coding Examples
Java Type Conversion - Upgrade your skills with coding Examples
Data Types in Java - Learn about primitive & non-primitive Java Data Types
Data Types in Java - Learn about primitive & non-primitive Java Data Types
Java Data Types Explained
Java Data Types Explained
Data Types in Java Explained | Primitive & Non-Primitive Java Data Types
Data Types in Java Explained | Primitive & Non-Primitive Java Data Types
three types of variable type in an organization
three types of variable type in an organization
Data Types in Java
Data Types in Java
typescript basic
typescript basic
Java Data Types Cheat Sheet 💻🔥 | Beginner Friendly
Java Data Types Cheat Sheet 💻🔥 | Beginner Friendly
String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics
The OOP'S Concept in JAVA🤔💯✅👇
The OOP'S Concept in JAVA🤔💯✅👇
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Constructor in Java with Example | Java Constructor Definition, Types, Rules, Declarations - BTech Geeks
Constructor in Java with Example | Java Constructor Definition, Types, Rules, Declarations - BTech Geeks

It is also useful to convert a value from one type to another to perform operations that require a different data type.

Syntax of Explicit Type Casting

The syntax to do explicit type casting in Java is as follows:

dataTypeName variableName = (dataTypeName) expression;

Here, expression is the value or variable which you want to convert into another data type.

Examples of Explicit Type Casting

In the previous example, if we wanted to assign the value of y to z, we would do:

int z = (int) y;

Here, we are explicitly converting the double y to an int and assigning the value to z.

It's important to note that explicit type casting can sometimes lead to a loss of information if the data type isn't correctly compatible. For instance, converting a double to an int will truncate the decimal part.

Finally, type casting is a powerful feature in Java that provides more control over how our code interacts with data types. It's a key aspect of every Java programmer's toolbox and should be used judiciously.