Type casting in Java | Widening and narrowing conversions in Java
Java is a strongly typed language, i.e. it supports the conversion of one data type to another.
Some conversions are implicitly done whereas some 'type conversions' must be explicitly stated in the program. These type conversions require checks for type compatibility i.e.
some 'type conversions' can be checked at compile time whereas others may require an extra check at runtime.
In case of an incompatible operand, for e.g. assigning a double to an int, a cast must be used to explicitly indicate the type conversion.
The cast construct has the following syntax:
Here, the cast -'type' is applied to the value of the 'expression'.
This type of casting results in a new value of 'type' at runtime.
The term casting is used to indicate the application of the cast operator for explicit type conversion.
Few things to be remembered w.r.t casting:
1. Casting can be applied to primitive values as well as references.
2. Casting is not permitted between primitive data types and reference types.
3. Boolean values cannot be cast to other data values and vice versa.
Widening conversions in Java
In the case of primitive data types in Java, the value of a narrower data type can be converted to a value of a broader data type without any loss of information. This type of conversion is called widening primitive conversion. Widening conversions are usually done implicitly. In this type of conversion, we assign a value of a smaller data type to a bigger data type, for e.g assigning an int to double.
static int i;
static double d;
public static void main(String[] args)
{
i=10;
d=i; //assigning an int to double
System.out.println("d = "+d);
}
}
The output of the above program is
d = 10.0
However, if we try to assign a double to an int i.e. a broader datatype to a smaller datatype without explicit type casting, then the compiler will throw the error -
"incompatible types: possible lossy conversion from double to int".
Consider the below program:
static int i;
static double d;
public static void main(String[] args)
{
d=10;
i=d; //assigning a double to int without explicit type casting
System.out.println("i = "+i);
}
}
The output of the above program will be:
incompatible types: possible lossy conversion from double to int
Narrowing conversions in Java
Narrowing conversions are usually explicit conversions i.e. they require a cast. In this type of conversion, the value of a broader datatype is assigned to a narrower (smaller) data type.
This type of conversion results in loss of magnitude information.
static int i;
static double d;
public static void main(String[] args)
{
d=10;
i=(int)d; //assigning a double to int
System.out.println("i = "+i);
}
}
The output of the above program will be:
i = 10
Upcasting and downcasting in Java
Conversions are also defined for reference types in Java. Widening and narrowing conversions are also applicable in the case of reference types. Conversions up the inheritance hierarchy are called widening reference conversions, also known as upcasting. In upcasting, the conversion takes place from sub-type to supertype. Whereas, Conversions down the inheritance hierarchy are called narrowing reference conversions, also known as downcasting. In the case of downcasting, the conversion takes place from supertype to sub-type. Upcasting is allowed and doesn't require an explicit cast, whereas, downcasting requires an explicit cast.
Example of upcasting in Java
Consider the below Java program:
int i=200;
public void suprFunc(){
System.out.println("value of i in super class = "+i);
}
}
class subClass extends suprClass{
int i=9;
//overrride the super class func
public void suprFunc(){
System.out.println("value of i in sub class = "+i);
}
}
class demo{
public static void main(String[] args){
suprClass supr = new subClass(); //upcasting
supr.suprFunc();
}
}
The output of the above program will be:
value of i in sub class = 9
If we try to replace the code in the demo class with this:
subClass subc = new suprClass();
subc.suprFunc();
Then we'll get compile time error:
incompatible types: suprClass cannot be converted to subClass
subClass subc = new suprClass();
Example of downcasting in Java
Consider the below Java program:
int i=200;
public void suprFunc(){
System.out.println("value of i in super class = "+i);
}
}
class subClass extends suprClass{
int i=9;
//overrride the super class func
public void suprFunc(){
System.out.println("value of i in sub class = "+i);
}
}
class demo{
public static void main(String[] args){
suprClass supr = new subClass(); //upcasting
subClass subc = (subClass)supr;//downcasting
subc.suprFunc();
}
}
The output of the above program will be:
value of i in sub class = 9