For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
Type casting in Java is converting one data type into another. It helps developers use memory more efficiently and write accurate, clean code. Whether working with numbers, characters, or objects, type casting ensures your data behaves as you expect.
Java supports two types of casting: implicit (automatic) and explicit (manual). Understanding when and how to use each is important for writing error-free programs. It becomes even more useful when working with expressions, loops, or methods involving multiple data types.
In this blog, we’ll break down everything you need to know about type casting. From basic examples to advanced object casting, you’ll learn how to apply it effectively in real projects.
As you explore Java through structured software engineering courses, these core concepts become easier to master and apply in real-world applications.
Type casting in Java refers to the process of converting a value from one data type to another. Java is a strongly typed language that requires explicit type declarations for variables, making type casting an essential operation when working with different data types. The Java type system is hierarchical, with more specific types at the bottom and more general types at the top.
Java supports two main categories of data types:
When it comes to type casting in Java, the rules differ significantly between primitive and reference types. Let's explore these differences in detail.
Before performing type casting in Java, it's essential to understand type compatibility. Two types are compatible if:
Not all types in Java are compatible with each other. For example, you cannot cast between boolean and numeric types, nor can you cast between incompatible reference types without a relationship in the inheritance hierarchy.
Advance your career with these proven skill-building programs.
Implicit type casting, also known as automatic type conversion or widening conversion, occurs when the Java compiler automatically converts a smaller data type to a larger data type without any explicit instruction from the programmer. This conversion happens without any potential loss of data.
The Java compiler performs implicit type casting when:
The automatic type conversion follows this hierarchy (from smallest to largest):
byte → short → int → long → float → double
Additionally, char can be implicitly cast to int and larger numeric types.
Also explore: Char Array in Java
public class ImplicitCastExample {
public static void main(String[] args) {
// Implicit casting from int to double
int intValue = 100;
double doubleValue = intValue; // Implicit casting happens here
System.out.println("Int value: " + intValue);
System.out.println("Double value after implicit casting: " + doubleValue);
// Implicit casting from char to int
char charValue = 'A';
int asciiValue = charValue; // Char is implicitly cast to int
System.out.println("Char value: " + charValue);
System.out.println("ASCII value after implicit casting: " + asciiValue);
}
}
Output:
Int value: 100
Double value after implicit casting: 100.0
Char value: A
ASCII value after implicit casting: 65
In this example, the value of intValue is automatically converted to double when assigned to doubleValue. Similarly, the char value 'A' is implicitly converted to its ASCII value (65) when assigned to an int variable.
Implicit type casting in Java occurs in several scenarios:
Must explore: Arithmetic Operators in Java
Explicit type casting, also known as narrowing conversion, is required when converting a larger data type to a smaller one. This conversion might result in data loss, which is why Java requires programmers to explicitly acknowledge this risk by using the cast operator.
The syntax for explicit type casting in Java is:
(targetType) expression
public class ExplicitCastExample {
public static void main(String[] args) {
// Explicit casting from double to int
double doubleValue = 100.99;
int intValue = (int) doubleValue; // Explicit casting required
System.out.println("Double value: " + doubleValue);
System.out.println("Int value after explicit casting: " + intValue);
// Explicit casting from long to int
long longValue = 922337203685L;
int intValue2 = (int) longValue; // Potential data loss
System.out.println("Long value: " + longValue);
System.out.println("Int value after explicit casting: " + intValue2);
}
}
Output:
Double value: 100.99
Int value after explicit casting: 100
Long value: 922337203685
Int value after explicit casting: 2147483647
Notice that in the first case, the decimal portion is truncated (not rounded), resulting in data loss. In the second case, the long value exceeds the int range, resulting in a completely different value due to overflow.
Explicit type casting in Java can lead to several issues:
In type casting in Java, conversions are categorized as either widening (implicit) or narrowing (explicit) based on the data capacity of the source and target types.
Widening conversions occur when converting a smaller type to a larger type. These conversions are safe and happen automatically because the target type can accommodate all possible values of the source type.
The standard widening conversions for primitive types are:
Source Type | Target Type |
byte | short, int, long, float, double |
short | int, long, float, double |
char | int, long, float, double |
int | long, float, double |
long | float, double |
float | double |
Narrowing conversions occur when converting a larger type to a smaller type. These conversions require explicit casting because they might result in data loss.
The standard narrowing conversions for primitive types are:
Source Type | Target Type |
short | byte, char |
char | byte, short |
int | byte, short, char |
long | byte, short, char, int |
float | byte, short, char, int, long |
double | byte, short, char, int, long, float |
A special case exists between char and byte/short. Although they are all 16-bit or smaller, you cannot implicitly cast between them because char is unsigned (0 to 65,535), while byte (-128 to 127) and short (-32,768 to 32,767) are signed.
Must explore: Memory Allocation in Java
Reference type casting in Java involves converting between reference types in an inheritance hierarchy. Unlike primitive type casting, which deals with data values, reference type casting deals with object references.
Upcasting is the process of casting a subclass reference to a superclass reference. This is always safe and happens implicitly because a subclass object is always an instance of its superclass.
// Upcasting example
class Animal { }
class Dog extends Animal { }
public class UpcastingExample {
public static void main(String[] args) {
Dog dog = new Dog();
Animal animal = dog; // Implicit upcasting
// Alternatively, explicit upcasting (redundant)
Animal animal2 = (Animal) dog;
}
}
Downcasting is the process of casting a superclass reference to a subclass reference. This requires explicit casting because not all superclass objects are instances of a specific subclass.
// Downcasting example
class Animal { }
class Dog extends Animal {
public void bark() {
System.out.println("Woof!");
}
}
public class DowncastingExample {
public static void main(String[] args) {
Animal animal = new Dog();
// Must use explicit downcasting
Dog dog = (Dog) animal;
dog.bark();
// ClassCastException scenario
Animal genericAnimal = new Animal();
try {
Dog impossibleDog = (Dog) genericAnimal; // Throws ClassCastException
} catch (ClassCastException e) {
System.out.println("Cannot cast Animal to Dog if it's not a Dog");
}
}
}
To avoid ClassCastException, you should use the instanceof operator before attempting to downcast:
public class SafeDowncasting {
public static void main(String[] args) {
Animal animal = new Dog();
// Safe downcasting with instanceof
if (animal instanceof Dog) {
Dog dog = (Dog) animal;
dog.bark();
}
Animal genericAnimal = new Animal();
if (genericAnimal instanceof Dog) {
Dog dog = (Dog) genericAnimal; // This block won't execute
} else {
System.out.println("Cannot downcast - not a Dog instance");
}
}
}
Java provides specific operators for type casting operations, both for primitive and reference types.
The primary operator for explicit type casting in Java is the cast operator, represented by parentheses containing the target type:
(targetType) expression
This operator can be used for both primitive and reference type casting.
The instanceof operator is used to check if an object is an instance of a particular class or implements a specific interface:
object instanceof Type
This operator returns a boolean value and is typically used before downcasting to prevent ClassCastException.
Java 16 introduced pattern matching for the instanceof operator, which simplifies the common pattern of type checking followed by casting:
// Traditional approach
if (obj instanceof String) {
String str = (String) obj;
// Use str
}
// Pattern matching approach (Java 16+)
if (obj instanceof String str) {
// Use str directly
}
Also explore: Polymorphism in Java
This example demonstrates a practical application of type casting in Java by creating a simple currency converter:
/**
* A simple currency converter demonstrating various type casting operations
*/
public class CurrencyConverter {
public static void main(String[] args) {
// Currency exchange rates (as of May 2023)
double usdToEurRate = 0.92;
double usdToGbpRate = 0.79;
double usdToJpyRate = 134.56;
// Amount in USD (as int)
int usdAmount = 100;
// Implicit casting: int to double
double usdAmountDouble = usdAmount;
// Calculate conversions
double eurAmount = usdAmountDouble * usdToEurRate;
double gbpAmount = usdAmountDouble * usdToGbpRate;
double jpyAmount = usdAmountDouble * usdToJpyRate;
// Display with 2 decimal places
System.out.println("Currency Converter Results:");
System.out.println("USD: $" + usdAmount);
System.out.println("EUR: €" + Math.round(eurAmount * 100) / 100.0);
System.out.println("GBP: £" + Math.round(gbpAmount * 100) / 100.0);
System.out.println("JPY: ¥" + Math.round(jpyAmount));
// Explicit casting: double to int (for whole units only)
int eurWholeUnits = (int) eurAmount;
int gbpWholeUnits = (int) gbpAmount;
int jpyWholeUnits = (int) jpyAmount;
System.out.println("\nWhole units only (explicit casting to int):");
System.out.println("EUR: " + eurWholeUnits);
System.out.println("GBP: " + gbpWholeUnits);
System.out.println("JPY: " + jpyWholeUnits);
}
}
Output:
Currency Converter Results:
USD: $100
EUR: €92.0
GBP: £79.0
JPY: ¥13456.0
Whole units only (explicit casting to int):
EUR: 92
GBP: 79
JPY: 13456
/**
* Temperature converter demonstrating multiple type casting scenarios
*/
public class TemperatureConverter {
public static void main(String[] args) {
// Starting with byte temperature in Celsius
byte celsiusTemp = 37; // Normal body temperature
// Implicit casting: byte to int
int celsiusIntTemp = celsiusTemp;
System.out.println("Celsius (byte): " + celsiusTemp);
System.out.println("Celsius (int): " + celsiusIntTemp);
// Formula: F = C * 9/5 + 32
// Note: Using 9.0/5 for precision
float fahrenheitTemp = celsiusTemp * 9.0f/5 + 32;
System.out.println("Fahrenheit (float): " + fahrenheitTemp);
// Explicit casting: float to int (truncates decimal)
int fahrenheitWholeTemp = (int) fahrenheitTemp;
System.out.println("Fahrenheit (int): " + fahrenheitWholeTemp);
// Kelvin = Celsius + 273.15
double kelvinTemp = celsiusTemp + 273.15;
System.out.println("Kelvin (double): " + kelvinTemp);
// Explicit casting with rounding
int kelvinRounded = (int) Math.round(kelvinTemp);
System.out.println("Kelvin rounded (int): " + kelvinRounded);
// Demonstrating potential data loss
double extremeTemp = 5000.99;
short extremeTempShort = (short) extremeTemp;
System.out.println("Extreme temp (double): " + extremeTemp);
System.out.println("After casting to short: " + extremeTempShort);
}
}
Output:
Celsius (byte): 37
Celsius (int): 37
Fahrenheit (float): 98.6
Fahrenheit (int): 98
Kelvin (double): 310.15
Kelvin rounded (int): 310
Extreme temp (double): 5000.99
After casting to short: 5000
Type casting in Java is a powerful mechanism that allows developers to convert between different data types, enabling more flexible and efficient code. Understanding the differences between implicit and explicit type casting, widening and narrowing conversions, and reference type casting is essential for writing Java applications. By following best practices and being aware of common pitfalls, you can avoid runtime errors and data loss issues associated with type casting.
Whether you're working with primitive data types or complex object hierarchies, proper type casting ensures type safety while allowing the flexibility needed for diverse programming scenarios. As you continue to develop your Java skills, remember that clear understanding of type casting concepts will help you write more efficient, error-free code that optimizes memory usage and performance.
Type casting and type conversion are related but not identical. Type conversion is a broader term that includes both implicit and explicit conversions. Type casting specifically refers to the explicit conversion using the cast operator. All type casting operations are type conversions, but not all type conversions are type casting operations.
No, Java only allows casting between compatible data types. For primitive types, you can cast between numeric types, but not between boolean and numeric types. For reference types, you can only cast between types in the same inheritance hierarchy (a class and its subclasses or superclasses, or classes implementing the same interface).
Explicit casting is required when there is a potential for data loss during the conversion, such as when converting from a larger data type to a smaller one. Java requires developers to acknowledge this risk by using the cast operator.
No, type casting does not modify the original variable. It creates a new value of the target type based on the original value. The original variable retains its original type and value.
Type casting operations, especially when done frequently, can have a performance impact. This is particularly true for boxing and unboxing operations (converting between primitive types and their wrapper classes). For critical code paths, it's best to minimize unnecessary type casting.
No, you cannot directly cast a String to an int using the cast operator. Instead, you need to use the Integer.parseInt() method or the Integer.valueOf() method, which is not type casting but type conversion.
When casting a float to an int, the decimal portion of the float is truncated (not rounded), and the resulting integer is the whole number portion of the float. For example, casting 3.7f to int results in 3, not 4.
To safely perform downcasting, always use the instanceof operator to check if the object is an instance of the target type before attempting to cast. This prevents ClassCastException at runtime.
(int) is a cast operator used for explicit type casting between compatible primitive types or reference types in the same inheritance hierarchy. Integer.parseInt() is a method that converts a String representation of a number to an int primitive type. They serve different purposes and cannot be used interchangeably.
Improper type casting, especially in combination with unchecked user input, can lead to security vulnerabilities. For instance, incorrect casting between object types might lead to type confusion attacks. Always validate input and check types before casting.
No, pattern matching for instanceof was introduced in Java 16. In older versions, you need to use the traditional approach of checking with instanceof and then performing an explicit cast.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author|900 articles published
Previous
Next
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.