---
title: "What is Java Typecasting?"  
description: "What is Java Typecasting?"  
author: "Nitin Kushwaha"  
published: 2019-07-10  
updated: 2025-02-24  
canonical: https://www.mindstick.com/forum/55156/what-is-java-typecasting  
category: "java"  
reading_time: 3 minutes  

---

# What is Java Typecasting?

**[Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) [Type Casting](https://www.mindstick.com/articles/1823/objective-c-type-casting):-**

Typecasting means that when we assign a one primitive [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) type to another data type.

## There are two types of casting:-

- [Implicit](https://www.mindstick.com/interview/827/what-are-implicit-objects-list-them) Type Casting.
- [Explicit](https://www.mindstick.com/interview/2677/can-you-explain-about-implicit-and-explicit-type-casting) Type Casting.

## Implicit Type Casting:-

- When we [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) smaller data type into a bigger data type.
- It can be done automatically.
- It is also [known as](https://answers.mindstick.com/qa/35703/ricky-ponting-is-also-known-as-what) “Widening Casting”.
- byte -> short -> char -> [int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql) -> [long](https://www.mindstick.com/articles/44565/white-wedding-dresses-long-prom-dresses) -> [float](https://www.mindstick.com/interview/1718/describe-float-containment) -> double.

## Example

```
 class MyClass {
  public static void main(String[] args) {
    int myInt = 25;
    double myDouble = myInt; // Automatic casting: int to double

    System.out.println(myInt);      // Outputs 25
    System.out.println(myDouble);   // Outputs 25.0
  }
}
```

## Explicit Type Casting:-

- When we convert bigger data type into a smaller data type that type of casting is known as explicating typecasting.
- It can be done manually.
- It is also known as “Narrowing Casting”.
- double -> float -> long -> int -> char -> short -> byte.

## Example

```
 class MyClass {
  public static void main(String[] args) {
    double myDouble = 22.71;
    int myInt = (int) myDouble; // Manual casting: double to int
    System.out.println(myDouble);   // Output: 22.71
    System.out.println(myInt);      // Outputs 22
  }
}
```

## \

## Replies

### Reply by Khushi Singh

**Typecasting in Java** systems changes data type from one to another type. We use this process to let variables of different data types hold one type's value. Java includes two approaches to convert data types.

## Implicit Typecasting (Widening Casting)

## Explicit Typecasting (Narrowing Casting)

## 1. Implicit Typecasting (Widening Casting)

- The operation happens seamlessly when you convert a smaller type to a larger type.\
- Since the larger type can contain all values found in the smaller type there is no chance of data loss.\
- Example

```java
int num = 50;
double convertedNum = num; // Implicit typecasting
System.out.println(convertedNum); // Output: 50.0
```

## 2. Explicit Typecasting (Narrowing Casting)

- When you purposefully change a large data type to fit into a smaller one you perform this manual operation.\
- The process leads to possible data loss because smaller variable types do not fully support bigger data types.\
- Users need to specify typecasting operations using (type) syntax.\
- Example

```java
double num = 99.99;
int convertedNum = (int) num; // Explicit typecasting
System.out.println(convertedNum); // Output: 99 (decimal part is lost)
```

Typecasting is essential in Java when working with different data types and performing precise conversions between them.

\


---

Original Source: https://www.mindstick.com/forum/55156/what-is-java-typecasting

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
