Type Casting
Type Casting
Type Casting:
It is process of converting data from one type into another type is
called as type casting
Programmer Compiler
(Explicitly) (Implicitly-during compile time by
compiler)
(Can’t store)
Bottle
Water
From the about example we are changing type of material that we store
but not type of container
int a; a
We had created a container ‘a’ that can only store int type of data
int a=10; a 10
//int = int
(As we are creating int type of container we can only store int type of data)
int a=10.0;
int = double X(Not possible)
(As java is strightly typed language If we try to store double type data inside int
type container we will get compile time error as “incompatible”
if we want to store then we need to change the type of data but not type of
container)
Type casting
Primitive Type-Casting:
The process of converting one type of primitive data into another type is
called as primitive type casting
o We have 8-primitive datatypes with their sizes in bytes
1. byte -1
2. short -2
3. int -4
4. long -8
5. float -4
6. double-8
7. char-2
8. boolean
Widening
byte<short<int<long<float<double
1 < 2 <4< 8< 4 < 8
Narrowing
1.class TypeCasting {
public static void main(String[] args) {
int res=10; //size of int is==>4
long l=res; //size of long is==>8 //Storing smaller range data into
//large container(Widening,
//No data loss)
//conversion done by compiler
System.out.println(l);
}
}
Output:
10
Task:
Fill the table below with YES/NO, Observe the output
if we can performing widening then fill with YES else fill with NO
Container/Variable type
Data byte short int long float double char boolean
byte
short
int
long
float
double
char
boolean
Ex:
Container/Variable type
Data int
short YES/NO
Take short type of data and place it inside int type of container
class Demo{
public static void main(String[ ] args )
{
short s=40;
int i=s;
Syste.out.println(i);
}
}
Narrowing: (Real time example: Shifting from large house into small)
The process of converting larger range(size) data into smaller range
data is known as Narrowing
o There is possibility of data loss
o Narrowing can‘t done by compiler implicitly(As there is chance of
data loss)
o We/Developer need’s to convert explicitly
Program:
class TypeCasting {
public static void main(String[] args) {
double res=10;//size of int is==>8
long l=res;//size of long is==>4
//Storing larger range data into
//small container(Narrowing,
//Possible of data loss)
//conversion can’t done by compiler
System.out.println(l);
}
}
Output:
TypeCasting.java:5: error: incompatible types: possible lossy conversion from
float to long
long l=res;//size of long is==>8
^
1 error
To convert larger range data to smaller range data (Narrowing) we will take help
of “type casting operator” for explicit conversion
Typecasting:
o It is a unary operator which accepts only one operand as input
o It is used to convert larger range data to smaller range data
(Narrowing) Explicitly (by developer/programmer)
o Explicit TypeCasting is nothing but programmer is forcefully
converting from one type of data to another type of data with the
help of TypeCasting operator
Syntax:
(type) value/variable/expression;
Program:
class TypeCasting {
public static void main(String[] args) {
double res=10.0;//size of int is==>8
long l=(long)res;//size of long is==>4
//Using TypeCast operator
System.out.println(l);
}
}
Output:
10
Program :
class TypeCasting2{
}
Output:
10.0
Program :
class TypeCasting{
Output:
3
Note :
We can also perform widening(Which is implicitly done by compiler) using
TypeCast operator (Explicitly)
class TypeCasting{
}
Output:
10.0
Program-1:
class TypeCasting{
Output: 25
Program-2:
class TypeCasting2{
Output: 65
Task:
Now fill the table below by using TypeCasting operator only
Container/Variable type
Data byte short int long float double char boolean
byte
short
int
long
float
double
char
boolean