Java provides various data types just like any other dynamic languages 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.
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
class GFG {
public static void main(String[] args)
{
int i = 100 ;
long l = i;
float f = l;
System.out.println( "Int value " + i);
System.out.println( "Long value " + l);
System.out.println( "Float value " + f);
}
}
|
Output
Int 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
public class GFG {
public static void main(String[] argv)
{
char ch = 'c' ;
int num = 88 ;
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
public class GFG {
public static void main(String[] args)
{
double d = 100.04 ;
long l = ( long )d;
int i = ( int )l;
System.out.println( "Double value " + d);
System.out.println( "Long value " + l);
System.out.println( "Int value " + i);
}
}
|
Output
Double 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
class GFG {
public static void main(String args[])
{
byte b;
int i = 257 ;
double d = 323.142 ;
System.out.println( "Conversion of int to byte." );
b = ( byte )i;
System.out.println( "i = " + i + " b = " + b);
System.out.println(
"\nConversion of double to byte." );
b = ( byte )d;
System.out.println( "d = " + d + " b= " + b);
}
}
|
Output
Conversion 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
class GFG {
public static void main(String args[])
{
byte b = 42 ;
char c = 'a' ;
short s = 1024 ;
int i = 50000 ;
float f = 5 .67f;
double d = . 1234 ;
double result = (f * b) + (i / c) - (d * s);
System.out.println( "result = " + result);
}
}
|
Output
result = 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
class GFG {
public static void main(String args[])
{
byte b = 50 ;
b = ( byte )(b * 2 );
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
15 Mar, 2023
Like Article
Save Article