Type Conversion and Casting in Java

Type Conversion:
As we know already type conversion is nothing but of convert the given data type to the specific datatype.
For Example:
int to long type conversion is possible (ie smaller size to larger size data conversion is done automatically)
but, double to byte type conversion is not possible


Java’s Automatic Conversions
When one type of data is assigned to another type of variable, an automatic type conversion
will take place if the following two conditions are met:
• The two types are compatible.
• The destination type is larger than the source type.


class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;

System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);

System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);

System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);

}


Output:
Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Conversion of double to byte.
d and b 323.142 67