Type casting in java allow the users to convert one form of data type into another but it should be between same " type " that means we cannot type primitive type to String or any other reference type and vice-versa .Think with respect to memory and the value storage inside memory. Compiler will type cast the value within its type only.
Type casting is of two types:-
1> Implicit type casting ( details are here beneath )
2> Explicit type casting.
Why compiler type cast within its type only?
It is because of the memory and the way value get stored in the memory. Lets take an example to make you more clear.
For ex-
We all know that byte has a 4 byte memory storage where as int has 8 byte memory storage , now think
is it possible to store 8 byte value into 4 byte?
answer is No because 8 byte memory value cannot accommodate within 4 byte memory. It can only accommodate within 8 byte memory or higher memory.
Here come the role of type casting , means with the help of it we can be able to do the same but only problem is that there " will be some data loss ".
Suppose there are two boxes, bigger box contain 8 balls and we want to keep all those box into its small box and if we try to keep all those balls into a smaller box then there may be small box left to accommodate.
Like this same is happening with the bits only. If we consider the above example then now we can say that from 8 byte memory only the 4 byte data will be accommodated to its lower type memory i.e of 4 byte.
Why we cannot type cast a primitive type to String type and vice versa?
We all know that type casting is concerned with the way of memory storage only and in case of other types there may be change of different memory implementation and the way of storage data into its memory is a bit different so type casting is not possible within other types.
Implicit type casting
It means automatically the value will get type casting by the compiler or JVM means we are forcing compiler to type cast.Here compiler is responsible for type casting.
for ex-
byte b = ' A ' ; // storing a char value to a byte type but it should be within the range of byte. Here compiler will internally type cast from char to int.
Type cast means-- compiler will just check whether the above statement is possible or not.
Explicit type casting.
Here we are forcing the compiler or JVM to type cast. Here user is responsible for type casting. There is some syntax user need to follow for typecasting.
Syntax :-
(Data_type) (variable) = [ (new_type) ] (value) ;
byte b = (byte)20 ;
Here in the above example value 20 is int type and it get converted into byte type with the help of following syntax.
int ab = 20 ;
byte b = (byte)ab ;
Here also variable ab containing int value get converted into byte type explicitly.
byte b = (byte)20.34 ; // compile successfully and float value typecasts to byte.
int ab = 20 ;
String s = (String) ab ;
The above code will get the compilation error " Incompatible type " that means int cannot be typecasts into String type.
According to data type we have two more types of typecasting i.e
1> Narrowing Casting
2> Widening Casting
Narrowing Casting
It is the type of casting in which we are forcing compiler to type cast a higher / wider type to its lower type.
For ex-
If we type cast a int type to byte type then its a narrowing type casting.
int ab = 20 ;
byte b = (byte)ab ;
Widening Casting
It is a type of casting in which we are forcing compiler to type a lower type to its higher / wider type.
For ex-
If we are storing a char value to a int type
char ch = 'A' ;
int a = ch ;
If we are storing a int value to a long type
long a = 4545 ;
Rule to be followed while Explicit type casting :
1> Type casting should be within the convertible data types.
for ex-
Boolean cannot be converted into other types and vice-versa.
boolean b = true;
int a = (int) b ;
2> Any primitive data type can't be converted into string.
for ex-
int cannot be converted into String type and vice-versa.
int ab = 20 ;
String s = (String) ab ;
0 comments:
Post a Comment