TYPE CASTING
- Converting one data type into another type is known as type casting.
- There are three types of type casting we have
- Primitive type casting
- Non primitive type casting
- Primitive to non primitive type casting
Primitive type casting:
- Converting from one primitive data type to another primitive data type is known as primitive type casting
- There are two types of primitive type casting
- Widining
- Narrowing
Widining:
- Conversion from one smaller primitive data type into the bigger primitive data type is known as widining.
- Widining happens automatically by the compiler.
- In there is no data loss hence it is known as implicit (Internal) type casting.
Program
- class TypeCasting
- {
- public static void main(String [] args)
- {
- byte b= 24;
- byte a= b;
- {
- System.out.println(a);
- }
- }
- }
Output:
24
Narrowing:
- Conversion from one bigger primitive data type to a smaller primitive data type is known as narrowing.
- We can actually narrowing with the help of type cast operator.
- In narrowing we will be e having data loss hence it is also known as explicit type casting.
Type cast operator:
- Conversion from one data type to another data type is respective of size.
Syntax
date type varname = (converting data type) vaiable/value
- class TypeCastOperator
- {
- public static void main(String [] args)
- {
- int b= 1024;
- byte a=(byte) b;
- {
- System.out.println(a);
- }
- }
- }
Output:
1024
0 Comments