Java Type Casting
There are different data type in Java, so type casting is the process of converting from one datatype to another datatype.
In Java there are 2 type of type casting:
- Implicit/ Automatic/ Widening Casting/ Upcasting
- Explicit/ Manual/ Narrowing Casting/ Downcasting
Implicit/ Automatic/ Widening Casting/ Upcasting:
- Widening typecasting is the automatic process of converting a smaller data type into a larger data type by the Java compiler. For this, we do not need to mention the casting explicitly.
- In implicit type casting, there is no risk of data loss because the value is automatically converted to a higher (wider) data type.
Example:
public class TypeCasting {
public static void main(String[] args) {
int myInteger = 15;
double myDouble = myInteger; // Implicit casting: int to double.
System.out.println(myInteger); // Outputs 15
System.out.println(myDouble); // Outputs 15.0
}
}Type promotion:
Java automatically changes a small data type into a bigger data type when doing calculations.
That means during calculation when the datatype is not the same then it promote one datatype(lower) to another datatype(higher).
Example-1:
public class TypeCasting {
public static void main(String[] args) {
byte myByte1 = 10; // byte is a smaller data type
byte myByte2 = 20; // byte is a smaller data type
int result = myByte1 + myByte2; // Here myByte1 and myByte2 are promoted to int before addition
System.out.println(result);//output : 30 int type
}
}So here in Example-1 even though myByte1 and myByte2 are of type byte, Java automatically promotes them to int before performing the addition. As a result, the final output is of type int.
Example-2:
public class TypeCasting {
public static void main(String[] args) {
int myInt1 = 10; // int is a smaller data type
double myDouble1 = 6.5; // double is a bigger data type
double result1 = myInt1 + myDouble1; // Here myInt1 is promoted to double before addition
System.out.println(result1); //output : 16.5 double type
}
}Here in Example-2, myInt1 is promoted to double, and the result is a double
So there is a Promotion Rules :
- byte, short, and char are promoted to int.
- If one operand is long, the result becomes long.
- If one operand is float, the result becomes float.
- If one operand is double, the result becomes double.
Example-3:
public class TypeCasting {
public static void main(String[] args) {
char c = 'A'; // char is a smaller data type
int x = c + 1; // Here char is promoted to int ('A' = 65), then 1 is added
System.out.println(x);// output : 66 int type
}
}Here char is promoted to int ('A' = 65), then 1 is added.
Explicit/ Manual/ Narrowing Casting/ Downcasting:
Narrowing casting is the process of converting a larger data type into a smaller data type manually using a cast operator ( ) in Java.
In narrowing casting, data loss can happen in some cases (not always).
Example-1:
public class TypeCasting {
public static void main(String[] args) {
double x = 9.78; // double is a larger data type
int y = (int) x; // Here double is explicitly cast to int, which may cause data loss
System.out.println(y);// output : 9, Decimal part is removed and Data loss happens
}
}Example-2:
public class TypeCasting {
public static void main(String[] args) {
long a = 10000L; // long is a larger data type
int b = (int) a; // Here long is explicitly cast to int, which may cause data loss if the value is too large for int
System.out.println(b);// output : 10000,Works, but may lose data if value is too large for int
}
}Usage :
- Convert incompatible types. Here, the compiler cannot safely convert the type automatically, so we did it explicitly
Example-1:
Without type casting
public class TypeCasting {
public static void main(String[] args) {
double num = 10.75; // double is a larger data type
int value = num; // Here double is implicitly cast to int, which is not allowed and will cause a compile-time error
System.out.println(value);
}
}Example-2:
With type casting
public class TypeCasting {
public static void main(String[] args) {
double num = 10.75; // double is a larger data type
int value = (int) num; // Here double is explicitly cast to int
System.out.println(value);
}
}- Controlling Program Behavior with Type Casting You choose how conversion happens instead of relying on defaults. Example:
public class TypeCasting {
public static void main(String[] args) {
int a = 5;
int b = 2;
System.out.println(a / b); //output : 2 without type cast.
//The fractional part (.5) is discarded.
int a = 5;
int b = 2;
System.out.println((float) a / b);//Output : 2.5 with type cast.
//So here we explicitly cast a to float, which promotes the division to floating-point division, preserving the fractional part.
}
}- Type Casting When Using APIs and Libraries Many APIs and libraries return data as a generic type, such as Object. Because the compiler does not know its actual type, you must cast it to the expected type before use. Example:
public class TypeCasting {
public static void main(String[] args) {
Object userName = api.getUserName(); // Assume this returns an Object, but we know it's actually a String
String name = (String) userName; // Explicitly cast the Object to String before using it
System.out.println(name.toUpperCase());// Now we can safely call String methods(toUpperCase()) on name
}
}See you in next page....🙂