Data Types Conversion and Casting

In programming there can be situation when we required to change the data type of a variable to some other data type, this process of data type conversion is known as type conversion or type casting.

Types of type casting -

Type casting can be categorized into 2 categories based on whether the two types are compatible or not -

1. Compatible Type conversion -

Java perform automatic type conversion if two types are compatible, and destination type is larger than the source type. For example a long type is large enough to contain all valid int or short or byte type, similarly double is large enough to hold any valid float type.
This process of storing smaller data type values into larger compatible data type is known as widening.

Example -

 byte a=10;
 int b=a;
 float c=b;  
 // Note that float is larger than integer types (byte, short, int, long)
All numeric data types, integers (byte, short, int, long) and floating point type (float, double) are compatible with each other.
Note - char and boolean are not compatible with each other.

2. Incompatible Type Conversion -

If two types are not compatible then we can convert the source type into destination type as follows -
     type variable1 = (type) variable2;

Example -

int a=10;
byte b = (byte)a;
short c = (short)a;

This process of storing larger data type values into smaller data types is known as narrowing.

Note - We can cast integers as well as floating point types into any data type except boolean.

Example -

public class Demo 
 public static void main(String[] args) 
   byte a=10;
   int b = 297;
   float c = 301.1f;
   char d = 'A';
   /* compatible type conversion */
   int e = a;  
   double f = c;
   float g = b;
   System.out.println("values of e, f and g are :"+e+"  "+f+"  "+g);
   /* Incompatible type conversion */
    byte h = (byte)b;
    float i = (float)f;
    System.out.println("values of h and i are :"+h+"  "+i);

Output -

values of e, f and g are :10  301.1000061035156  297.0
values of h and i are :41  301.1
Note :- the value of 'h' is 41 this is because the integer value 297 is out of the rang of byte, in such case value will be reduced by finding the modulus (reminder of division) by the byte range (256). (When 297 is cast to byte the result is reminder of the division of 297 by 256 i.e. 41).

Automatic Type Promotion in Expressions Evaluation -

While evaluating any expression containing byte, short or char operands, java automatically promotes them into int.

The reason behind this is that as range of these data types is limited, so while evaluating any expression there are high chances that result may go out of range of these data types, that?s why java automatically promote them into int to avoid such conditions.
For example -
byte a = 75;
byte b = 30;
int c = a*b; 
If we want the result of same data type than we have to manually type cast them as follows -
byte a = 10;
byte c =(byte) a*5;
Note :- In any expression evaluation that contains variables of different data types, the result of evaluation will be promoted to largest data type available in expression.
In any expression first of all byte, short and char are promoted to int. Then if there is a long variable than whole expression will be promoted to long, similarly if there is any float variable then whole expression will be promoted to float and so on.

Standard Default values -

When we declared variables for the first time, java automatically assign some default values to them as shown below -
Data type Default Value
byte 0
short 0
int 0
long 0
float 0.0f
double 0.0d
char '\u0000' (nothing)
boolean false