Type Conversion in Java with Examples
Last Updated :
24 Mar, 2025
Java provides various data types just like any other dynamic language such as boolean, char, int, unsigned int, signed int, float, double, long, etc in total providing 7 types where every datatype acquires different space while storing in memory. When you assign a value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion, and if not then they need to be cast or converted explicitly. For example, assigning an int value to a long variable.Â
Data Types and Their Memory Allocation
Datatype | Bits Acquired In Memory |
---|
boolean | 1 |
byte | 8 (1 byte) |
char | 16 (2 bytes) |
short | 16(2 bytes) |
int | 32 (4 bytes) |
long | 64 (8 bytes) |
float | 32 (4 bytes) |
double | 64 (8 bytes) |
Widening or Automatic Type Conversion
Widening conversion takes place when two data types are automatically converted. This happens when:Â Â
- The two data types are compatible.
- When we assign a value of a smaller data type to a bigger data type.
For Example, in java, the numeric data types are compatible with each other but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other.Â

Example:
Java
// Java Program to Illustrate Automatic Type Conversion
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
int i = 100;
// Automatic type conversion
// Integer to long type
long l = i;
// Automatic type conversion
// long to float type
float f = l;
// Print and display commands
System.out.println("Int value " + i);
System.out.println("Long value " + l);
System.out.println("Float value " + f);
}
}
OutputInt value 100
Long value 100
Float value 100.0
Narrowing or Explicit Conversion
If we want to assign a value of a larger data type to a smaller data type we perform explicit type casting or narrowing. Â
- This is useful for incompatible data types where automatic conversion cannot be done.
- Here, the target type specifies the desired type to convert the specified value to.
Â

char and number are not compatible with each other. Let's see when we try to convert one into another.Â
Java
// Java program to illustrate Incompatible data Type
// for Explicit Type Conversion
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv)
{
// Declaring character variable
char ch = 'c';
// Declaringinteger variable
int num = 88;
// Trying to insert integer to character
ch = num;
}
}
Output: An error will be generated

This error is generated as an integer variable takes 4 bytes while character datatype requires 2 bytes. We are trying to plot data from 4 bytes into 2 bytes which is not possible.
How to do Explicit Conversion?Â
Java
// Java program to Illustrate Explicit Type Conversion
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Double datatype
double d = 100.04;
// Explicit type casting by forcefully getting
// data from long datatype to integer type
long l = (long)d;
// Explicit type casting
int i = (int)l;
// Print statements
System.out.println("Double value " + d);
// While printing we will see that
// fractional part lost
System.out.println("Long value " + l);
// While printing we will see that
// fractional part lost
System.out.println("Int value " + i);
}
}
OutputDouble value 100.04
Long value 100
Int value 100
Note: While assigning value to byte type the fractional part is lost and is reduced to modulo 256(range of byte).Â
Example:
Java
// Java Program to Illustrate Conversion of
// Integer and Double to Byte
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Declaring byte variable
byte b;
// Declaring and initializing integer and double
int i = 257;
double d = 323.142;
// Display message
System.out.println("Conversion of int to byte.");
// i % 256
b = (byte)i;
// Print commands
System.out.println("i = " + i + " b = " + b);
System.out.println(
"\nConversion of double to byte.");
// d % 256
b = (byte)d;
// Print commands
System.out.println("d = " + d + " b= " + b);
}
}
OutputConversion of int to byte.
i = 257 b = 1
Conversion of double to byte.
d = 323.142 b= 67
Type Promotion in Expressions
While evaluating expressions, the intermediate value may exceed the range of operands and hence the expression value will be promoted. Some conditions for type promotion are:Â Â
- Java automatically promotes each byte, short, or char operand to int when evaluating an expression.
- If one operand is long, float or double the whole expression is promoted to long, float, or double respectively.
Example:
Java
// Java program to Illustrate Type promotion in Expressions
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Declaring and initializing primitive types
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
// The Expression
double result = (f * b) + (i / c) - (d * s);
// Printing the result obtained after
// all the promotions are done
System.out.println("result = " + result);
}
}
Outputresult = 626.7784146484375
Explicit Type Casting in Expressions
While evaluating expressions, the result is automatically updated to a larger data type of the operand. But if we store that result in any smaller data type it generates a compile-time error, due to which we need to typecast the result.Â
Example:
Java
// Java program to Illustrate Type Casting
// in Integer to Byte
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Declaring byte array
byte b = 50;
// Type casting int to byte
b = (byte)(b * 2);
// Display value in byte
System.out.println(b);
}
}
Note: In case of single operands the result gets converted to int and then it is typecast accordingly, as in the above example.
Difference Between Explicit and Implicit Conversion
The below table demonstrates the key difference between Explicit and Implicit Conversion
Aspect | Implicit Conversion | Explicit Conversion |
---|
Syntax | No additional syntax required. | Requires explicit type casting (e.g., (int)). |
---|
Data Compatibility | Only works with compatible types (e.g., numeric). | Works with both compatible and incompatible types. |
---|
Risk of Data Loss | No risk of data loss. | May result in data loss (e.g., truncation). |
---|
Direction | Smaller type to larger type. | Larger type to smaller type. |
---|
Note:
- Use implicit conversion when assigning a value of a smaller data type to a larger data type (e.g., int to long).
- Use explicit conversion when assigning a value of a larger data type to a smaller data type (e.g., double to int) or when converting between incompatible types.
Similar Reads
Closures in Java with Examples
A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class that is diffe
5 min read
Collections list() method in Java with Examples
The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that requi
2 min read
Different Types of Classes in Java with Examples
A class is a user-defined blueprint or prototype from which objects are created. Â It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order: Â Modifiers: A class can be public or has default access
11 min read
Collection add() Method in Java with Examples
The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet
4 min read
Throwable Class in Java with Examples
Classes and Objects are basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In this article,
7 min read
Calendar Class in Java with examples
Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable, Serializable, Cloneable interfaces. As it is an Abstract class
4 min read
Collection vs Collections in Java with Example
Collection: Collection is a interface present in java.util package. It is used to represent a group of individual objects as a single unit. It is similar to the container in the C++ language. The collection is considered as the root interface of the collection framework. It provides several classes
3 min read
Open Closed Principle in Java with Examples
In software development, the use of object-oriented design is crucial. It helps to write flexible, scalable, and reusable code. It is recommended that the developers follow SOLID principles when writing a code. One of the five SOLID principles is the open/closed principle. The principle states that
9 min read
Bounded Types with Generics in Java
There may be times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that operates on numbers might only want to accept instances of Numbers or their subclasses. This is what bounded type parameters are for. Sometimes we donât want
4 min read
Arrays asList() Method in Java with Examples
The Arrays.asList() method in Java is part of the java.util.Arrays class, which is used to convert an array into a fixed-size list. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.toArray(). Example:Below is a simple example of the Arrays as
4 min read