Implicit & Explicit Casting

Type Casting

It is use to convert one type into another type

Primitive Casting
  • Casting lets you convert primitive values from one type to another.
  • Casts can be implicit (Widening Conversion) or explicit (Narrowing Conversion)
Primitive Casting ( Implicit )
int a; //take 4 bytes in memory
byte b=20; //take 1 byte in memory
a=b; //Implicit cast ( this cast conversion happens automatically , because bigger type can hold smaller type)
float b;
int a=90;
Primitive Casting ( Explicit )
int d=90;
byte a=(byte) d;
System.out.println(a); //print 90
int d=130;
byte a=(byte)d; //Print -126
float a=90.10f;
int b;
b=(int) a; //explicit cast (convert bigger type into smaller type), and programmer has to do explicitly