In This Article, You Will Learn about Java Type Casting.
Java Type Casting – Before moving ahead, let’s know a bit about Java Data Types.
Table of Contents
Java Type Casting
In Java, Type Casting is the process of assigning a value of one primitive data type to another type.
Types of Casting
Widening Casting (automatically)
It converts a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually)
It converts a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
Illustrations:
Widening Casting – It does casting automatically from a smaller size type to a larger size type.
public class Main {
public static void main(String[] args) {
int Myint = 10;
float Mydouble = Myint;
System.out.println(Myint);
System.out.println(Mydouble);
}
}
Narrowing Casting – It works manually by placing the data type in parentheses in front of the value.
public class Main {
public static void main(String[] args) {
int Myint = 10;
double Mydouble = (double) Myint;
System.out.println(Myint);
System.out.println(Mydouble);
}
}
A few examples of Type casting –
Example: Conversion from int to float.
public class Hello_World{
public static void main(String[] args) {
int myInt = 10;
float myFloat = myInt;
System.out.println(myInt);
System.out.println(myFloat);
}
}
Example: Conversion from int to double.
public class Hello_World{
public static void main(String[] args) {
int myInt = 10;
double myDouble = myInt;
System.out.println(myInt);
System.out.println(myDouble);
}
}
Example: Conversion from byte to int.
public class Hello_World{
public static void main(String[] args) {
byte myByte = 10;
int myInt = myByte;
System.out.println(myByte);
System.out.println(myInt);
}
}
Example: Conversion from byte to float.
public class Hello_World{
public static void main(String[] args) {
byte myByte = 10;
float myFloat = myByte;
System.out.println(myByte);
System.out.println(myFloat);
}
}
Example: Conversion from byte to double.
public class Hello_World{
public static void main(String[] args) {
byte myByte = 10;
double myDouble = myByte;
System.out.println(myByte);
System.out.println(myDouble);
}
}
Example: Conversion from long to double.
public class Hello_World{
public static void main(String[] args) {
long myLong = 10000000000000000L;
double myDouble = myLong;
System.out.println(myLong);
System.out.println(myDouble);
}
}
Example: Conversion from long to float.
public class Hello_World{
public static void main(String[] args) {
long myLong = 10000000000000000L;
float myFloat = myLong;
System.out.println(myLong);
System.out.println(myFloat);
}
}
Example: Conversion from int to long.
public class Hello_World{
public static void main(String[] args) {
int myInt = 10;
long myLong = myInt;
System.out.println(myInt);
System.out.println(myLong);
}
}
Example: Conversion from short to int.
public class Hello_World{
public static void main(String[] args) {
short myshort = 100;
int myInt = myshort;
System.out.println(myshort);
System.out.println(myInt);
}
}
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.
Connect on: