The process of converting the value of one data type to another data type is known as type casting.
Widening Casting
Converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Byte to Short
Convert byte variable to short variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
byte b = 127;
short s = b;
System.out.println("short: " + s);
}
}
Output:
short: 127
Short to Char
Convert short variable to char variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
short s = 65;
char c = (char) s;
System.out.println("char: " + c);
}
}
Output:
char: A
Char to Int
Convert char variable to int variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
char ch = 'B';
int i = ch;
System.out.println("i: " + i);
}
}
Output:
i: 66
Int to Long
Convert int variable to long variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int i = 123;
long lo = i;
System.out.println("lo: " + lo);
}
}
Output:
lo: 123
Long to Float
Convert long variable to float variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
long lo = 123;
float f = lo;
System.out.println("f: " + f);
}
}
Output:
f: 123.0
Float to Double
Convert float variable to double variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
float f = 4.5f;
double d = f;
System.out.println("d: " + d);
}
}
Output:
d: 4.5
Best Desktops For Programming
Narrowing Casting
Converting a larger type to a smaller size type.
double -> float -> long -> int -> char -> short -> byte
Double to Float
Convert double variable to float variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
double d = 5.6;
float f = (float) d;
System.out.println("f: " + f);
}
}
Output:
f: 5.6
Float to Long
Convert float variable to long variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
float f = 5.6f;
long lo = (long) f;
System.out.println("lo: " + lo);
}
}
Output:
lo: 5
Long to Int
Convert long variable to int variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
long lo = 456;
int i = (int) lo;
System.out.println("i: " + i);
}
}
Output:
i: 456
Int to Char
Convert int variable to char variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int i = 71;
char ch = (char) i;
System.out.println("ch: " + ch);
}
}
Output:
ch: G
Char to Short
Convert char variable to short variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
char c = 'D';
short s = (short) c;
System.out.println("s: " + s);
}
}
Output:
s: 68
Short to Byte
Convert short variable to byte variable
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
short s = 127;
byte b = (byte) s;
System.out.println("b: " + b);
}
}
Output:
b: 127