What is Type Casting in Java?
A data type is a predefined set of values that specify the type of values that can be stored in it along with the operation that can be carried out on them.Why is Type Casting Required?
Java has different primitive data types which require different spaces in memory. This can cause compatibility issues while assigning a value of one data type to another. If the datatypes are already compatible, typecasting is done automatically by the compiler. Thus, typecasting solves a major compatibility problem when dealing with different data types in our program.Types of Java Type Casting
There are two types of type casting in Java.- Widening Type Casting — Also known as Implicit or Automatic Type Casting
- Narrowing Type Casting — Also known as Explicit or Manual Type Casting
Widening Type Casting
Widening typecasting, as the name suggests, refers to the widening of a smaller data type to a larger data type. We perform this typecasting when we want to convert a small type to a large type. The data types must be compatible with each other. There is no implicit conversion from numeric to char or boolean type. In Java, char and boolean types are non-compatible.byte -> short -> char -> int -> long -> float -> double
This type of casting is done automatically by the compiler without any loss of information. It does not require any external trigger by the programmer.
Example
//Automatic type conversion
public class WideningExample {
public static void main(String[] args) {
int i = 100;
System.out.println("int value: " + i);
// int to long type
long l = i;
System.out.println("int to long value: " + l);
// long to float type
float f = l;
System.out.println("int to float value: " + f);
byte b = 1;
System.out.println("byte value: " + b);
// byte to int type
i = b;
System.out.println("byte to int value: " + i);
char c = 'a';
System.out.println("char value: " + c);
// char to int type
i = c;
// prints the ASCII value of the given character
// ASCII value of 'a' = 97
System.out.println("char to int value: " + i);
}
}
Output
int value: 100
int to long value: 100
int to float value: 100.0
byte value: 1
byte to int value: 1
char value: a
char to int value: 97
Explanation
In the code above, we have shown widening typecasting which is done by the compiler automatically. First of all, we assigned values to an int, byte, and char. We then assigned the int values to a long and float, both of which are larger than int. We also assigned the byte and char values to int. Both byte and char are smaller data types than int, thus, these conversions were implicit.Narrowing Type Casting
Narrowing typecasting, as the name suggests, refers to the narrowing of a larger data type to a smaller data type. We perform this typecasting when we want to convert a large type to a small type.double -> float -> long -> int -> char -> short -> byte
For this type of casting, we override the Java’s default conversion by specifying our own conversion. To achieve this, we write the variable or value that needs to be typecasted preceded by the target data type in parentheses ‘()’. However, this type of casting may result in a possible loss of precision.
Example
//Manual Type Conversion
public class NarrowingExample {
public static void main(String[] arg) {
// double data type
double d = 97.04;
// Print statements
System.out.println("double value: " + d);
// Narrowing type casting from double to long
// implicitly writing the target data type in () followed by initial data type
long l = (long) d;
// fractional part lost - loss of precision
System.out.println("long value: " + l);
// Narrowing type casting from double to int
// implicitly writing the target data type in () followed by initial data type
int i = (int) l;
// fractional part lost - loss of precision
System.out.println("int value: " + i);
// Narrowing type casting from double to int
// implicitly writing the target data type in () followed by initial data type
char c = (char) i;
// displaying character corresponding to the ASCII value of 100
System.out.println("char value: " + c);
}
}
Output
double value: 97.04
long value: 97
int value: 97
char value: a
GO TO FULL VERSION