Table of contents
1.
Introduction
2.
What is Type Conversion?
2.1.
Java
3.
What is Type Casting?
3.1.
Java
4.
Data Type Hierarchy and Compatibility in Java
4.1.
Understanding Widening and Narrowing Conversions
4.1.1.
Examples of Widening Conversions:
4.1.2.
Examples of Narrowing Conversions:
4.2.
Primitive Types and Their Casting Possibilities
4.2.1.
Widening (Automatic) Conversions:
4.2.2.
Narrowing (Explicit) Conversions:
4.2.3.
Conversion Compatibility Table
5.
Difference Between Type Casting and Type Conversion
6.
Frequently Asked Questions
6.1.
What is meant by type conversion and type casting in Java?
6.2.
What are the common methods used to change data types in Java?
6.3.
What is explicit type conversion?
6.4.
What is automatic type conversion?
6.5.
Differentiate between type conversion and type casting in Java based on data loss.
7.
Conclusion
Last Updated: Sep 18, 2025
Easy

Type Conversion And Type Casting In Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A significant part of programming involves playing around with data. While performing some tasks, situations might arise where the data type has to be changed from one type to another. For example, while calculating the area of a circle using Pi, we get a decimal number, and that needs to be converted to integer if we desire to obtain the answer in integer format. 

Type Conversion And Type Casting In Java

Since Java is strongly typed, one often needs to convert from one data type to another. The programmer or the compiler can easily do this by using type casting and type conversion concepts.

In Java, there are thirteen types of type Conversions. You can explore more about them by referring to the Java Type Conversion section in the Official Oracle Java Documentation. This blog will dive deep into the concept of type conversion and type casting in Java in detail, along with some examples.

What is Type Conversion?

Type conversion is a process in which the data type is automatically converted into another data type. The compiler does this automatic conversion at compile time. The data type to which the conversion happens is called the destination data type, and the data type from which the conversion happens is called the source data type. If the source and destination data types are compatible, then automatic type conversion takes place.

For type conversion to take place, the destination data type must be larger than the source type. In short, the below flow chart has to be followed.

Flow chart for Type Conversion

Flow chart for Type Conversion

This type of type conversion is also called Widening Type Conversion/ Implicit Conversion/ Casting Down.  In this case, as the lower data types with smaller sizes are converted into higher ones with a larger size, there is no chance of data loss. This makes it safe for usage.

 type conversion converted into the destination data type

In type conversion, the source data type with a smaller size is converted into the destination data type with a larger size.

Before looking at an example for the type conversion type, let's see what happens when incompatible data types are given.

public class Main {
 public static void main(String[] args) {
   int intType = 20;
   // Short is of lower data type than int
   short shortType = intType;

   System.out.println("intType: "+intType);
   System.out.println("shortType: "+shortType);
 }
}
You can also try this code with Online Java Compiler
Run Code

 

Compile Time Error:

compile time error

In the above case, short is of lower data type than int, hence a compile-time error occurs. In the next section, we will see how to solve this error.

Example for Type Conversion in Java:

  • Java

Java

public class Main {
public static void main(String[] args) {
  int intType = 20;
  // Float is of higher data type than int
  float floatType = intType;

  System.out.println("intType: "+intType);
  System.out.println("floatType: "+floatType);
}
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

intType: 20
floatType: 20.0

What is Type Casting?

Type casting is a process in which the programmer manually converts one data type into another data type. For this the casting operator (), the parenthesis is used. Unlike type conversion, the source data type must be larger than the destination type in type casting. The below flow chart has to be followed for successful type casting.

Flow chart for Type Casting

Flow chart for Type Casting

 

Type casting is also called Narrowing Type Casting/ Explicit Conversion/ Casting Up. In this case, as the higher data types with a larger size are converted into lower ones with a smaller size, there is a chance of data loss. This is the reason that this type of conversion does not happen automatically.

type casting converted into the destination data type

In type casting, the source data type with a larger size is converted into the destination data type with a smaller size.

In one of the examples covered in type conversion, you might recollect we encountered an error while converting an int data type to short. Now, we will see how to solve that error by using type casting. 

Example for Type Casting in Java:

  • Java

Java

public class Main {
public static void main(String[] args) {
int intType = 20;
// Short is of lower data type than int
short shortType = (short)intType;
 System.out.println("intType: "+intType);
 System.out.println("shortType: "+shortType);
}
}
You can also try this code with Online Java Compiler
Run Code


Output:

intType: 20
shortType: 20

Also see, Duck Number in Java

Data Type Hierarchy and Compatibility in Java

Understanding Widening and Narrowing Conversions

In Java, widening and narrowing conversions describe how primitive data types can be converted into one another.

  • Widening conversions (also known as automatic or implicit conversions) occur when a smaller data type is converted to a larger data type. These conversions happen automatically and do not result in data loss.
  • Narrowing conversions (manual or explicit conversions) happen when a larger data type is converted to a smaller one. These conversions may cause data loss and must be done explicitly by the programmer.

Examples of Widening Conversions:

  • int a = 10; double b = a;
    The int value is automatically converted to double.
  • byte c = 20; int d = c;
    The byte value is widened to int without explicit casting.

Examples of Narrowing Conversions:

  • double x = 9.8; int y = (int) x;
    The fractional part is lost after converting double to int.
  • int m = 300; byte n = (byte) m;
    The int value is explicitly cast to byte, which may cause data overflow.

Primitive Types and Their Casting Possibilities

Java’s primitive data types in hierarchical order (from smallest to largest):

  1. byte (1 byte)
  2. short (2 bytes)
  3. char (2 bytes)
  4. int (4 bytes)
  5. long (8 bytes)
  6. float (4 bytes, single-precision)
  7. double (8 bytes, double-precision)

Widening (Automatic) Conversions:

  • byte → short → int → long → float → double
  • char → int → long → float → double

These conversions happen automatically because they are safe and there is no risk of data loss.

Narrowing (Explicit) Conversions:

  • double → float → long → int → short → byte
  • int → char and long → char require explicit casting

Narrowing conversions can lead to data loss, truncation, or overflow and must be explicitly handled by the programmer.

Conversion Compatibility Table

From \ Tobyteshortcharintlongfloatdouble
byteYesNoYesYesYesYes
shortNoNoYesYesYesYes
charNoNoYesYesYesYes
intNoNoNoYesYesYes
longNoNoNoNoYesYes
floatNoNoNoNoNoYes
doubleNoNoNoNoNoNo

 

  • Yes: Widening (automatic)
  • No: Requires explicit casting (narrowing)
  • : Same data type (no conversion needed)

Difference Between Type Casting and Type Conversion

ParameterType CastingType Conversion
DefinitionType casting is a process in which the programmer manually converts one data type into another data type.Type conversion is a process in which the data type is automatically converted into another data type
MethodExplicitly done by the programmer using casting operators.Can be implicit or explicit, depending on the language and context.
Syntax(newType) variableFunction calls, constructors, or other conversion methods.
CompatibilityMust be compatible data types. Can involve conversions between incompatible data types. 
ChangeImmediate change in the data type of the variable.The result may involve data loss or rounding errors, depending on the conversion m

Frequently Asked Questions

What is meant by type conversion and type casting in Java?

In type conversion, a compiler converts a lower data type into a higher data type. In typecasting, a higher data type is converted into a lower data type by the programmer.

What are the common methods used to change data types in Java?

Type Conversion and Type Casting in Java enable converting one data type into another.

What is explicit type conversion?

Explicit type conversion is when a higher data type is converted into a lower data type by the programmer explicitly.

What is automatic type conversion?

Automatic type conversion is when a compiler automatically converts a lower data type into a higher data type.

Differentiate between type conversion and type casting in Java based on data loss.

There is a chance of data loss in typecasting, while the data is safe in type conversion.

Conclusion

In conclusion, Type Conversion and Type Casting in Java are essential for handling data across different types. Type conversion (widening) occurs automatically when a smaller data type is assigned to a larger one, while type casting (narrowing) requires explicit conversion. Mastering these ensures accurate computations, optimized memory use, and better control over type safety in Java programs.

Recommended Readings:

 

Live masterclass